linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Mark kobjects as unitialized
@ 2008-03-06 16:53 Balaji Rao
  2008-03-06 17:05 ` Greg KH
  0 siblings, 1 reply; 18+ messages in thread
From: Balaji Rao @ 2008-03-06 16:53 UTC (permalink / raw)
  To: gregkh; +Cc: kvm-devel, linux-kernel

Hi greg,

When I remove only the kvm-intel module without removing the kvm module itself, I get an error saying that a kobject is 
trying to be reinitialized. Its because of the fact that kvm reuses a kobject in kvm_init when calling sysdev_register.

This patch fixes kobject_cleanup by marking the kobject as uninitialized when we cleanup to allow kobjects to be reused.

Signed-off-by: Balaji Rao <balajirrao@gmail.com>

diff --git a/lib/kobject.c b/lib/kobject.c
index 0d03252..fbdefb8 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -577,6 +577,9 @@ static void kobject_cleanup(struct kobject *kobj)
 		pr_debug("kobject: '%s': free name\n", name);
 		kfree(name);
 	}
+
+	/* Set the state to uninitialized */
+	kobj->state_initialized = 0;
 }
 
 static void kobject_release(struct kref *kref)

-- 
regards,
balaji rao
3rd year, Dept. of Mechanical Engineering,
National Institute of Technology, Karnataka

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-06 16:53 [PATCH] Mark kobjects as unitialized Balaji Rao
@ 2008-03-06 17:05 ` Greg KH
  2008-03-06 17:50   ` Balaji Rao
  0 siblings, 1 reply; 18+ messages in thread
From: Greg KH @ 2008-03-06 17:05 UTC (permalink / raw)
  To: Balaji Rao; +Cc: kvm-devel, linux-kernel

On Thu, Mar 06, 2008 at 10:23:18PM +0530, Balaji Rao wrote:
> Hi greg,
> 
> When I remove only the kvm-intel module without removing the kvm
> module itself, I get an error saying that a kobject is trying to be
> reinitialized. Its because of the fact that kvm reuses a kobject in
> kvm_init when calling sysdev_register.

Where exactly in the code does that happen?  kobjects should not be
"reused" as that implies that they are static, and not dynamically
allocated, right?

Which kobject is this?

Ugh, is this the sys_device stuff?  I hate that code...

> This patch fixes kobject_cleanup by marking the kobject as
> uninitialized when we cleanup to allow kobjects to be reused.

The patch seems correct, but I would like to verify which kobject is
being reused here first.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-06 17:05 ` Greg KH
@ 2008-03-06 17:50   ` Balaji Rao
  2008-03-06 18:05     ` Greg KH
  0 siblings, 1 reply; 18+ messages in thread
From: Balaji Rao @ 2008-03-06 17:50 UTC (permalink / raw)
  To: Greg KH; +Cc: kvm-devel, linux-kernel

On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
<snip>
> Where exactly in the code does that happen?  kobjects should not be
> "reused" as that implies that they are static, and not dynamically
> allocated, right?
>
> Which kobject is this?
Yes, its static. Here's the code from virt/kvm_main.c:1269

static struct sys_device kvm_sysdev = {
        .id = 0,
        .cls = &kvm_sysdev_class,
};

this sys_device is being registered/unregistered when kvm-intel is 
loaded/unloaded.
>
> Ugh, is this the sys_device stuff?  I hate that code...
>
Yes it is! But, why do you hate it ?

-- 
regards,
balaji rao
3rd year,
Dept. of Mechanical Engineering,
National Institute of Technology, Karnataka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-06 17:50   ` Balaji Rao
@ 2008-03-06 18:05     ` Greg KH
  2008-03-07  8:56       ` [kvm-devel] " Avi Kivity
  2008-03-08 22:07       ` Balaji Rao
  0 siblings, 2 replies; 18+ messages in thread
From: Greg KH @ 2008-03-06 18:05 UTC (permalink / raw)
  To: Balaji Rao; +Cc: kvm-devel, linux-kernel

On Thu, Mar 06, 2008 at 11:20:50PM +0530, Balaji Rao wrote:
> On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
> <snip>
> > Where exactly in the code does that happen?  kobjects should not be
> > "reused" as that implies that they are static, and not dynamically
> > allocated, right?
> >
> > Which kobject is this?
> Yes, its static. Here's the code from virt/kvm_main.c:1269
> 
> static struct sys_device kvm_sysdev = {
>         .id = 0,
>         .cls = &kvm_sysdev_class,
> };
> 
> this sys_device is being registered/unregistered when kvm-intel is 
> loaded/unloaded.

Ah, ok.  I'll add this patch then.

> > Ugh, is this the sys_device stuff?  I hate that code...
> >
> Yes it is! But, why do you hate it ?

For reasons like this :)

kobjects should not be static.  the sysdevice stuff was a hack when it
was originally created and never touched since the mid 2.5 days.  It
needs to be fixed up a lot, and is on my TODO list, slowly getting
closer to the top...

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [kvm-devel] [PATCH] Mark kobjects as unitialized
  2008-03-06 18:05     ` Greg KH
@ 2008-03-07  8:56       ` Avi Kivity
  2008-03-08 22:07       ` Balaji Rao
  1 sibling, 0 replies; 18+ messages in thread
From: Avi Kivity @ 2008-03-07  8:56 UTC (permalink / raw)
  To: Greg KH; +Cc: Balaji Rao, kvm-devel, linux-kernel

Greg KH wrote:
> and is on my TODO list, slowly getting
> closer to the top...
>
>   

Strange.  On my TODO list, things slowly get pushed to the bottom.

-- 
Any sufficiently difficult bug is indistinguishable from a feature.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-06 18:05     ` Greg KH
  2008-03-07  8:56       ` [kvm-devel] " Avi Kivity
@ 2008-03-08 22:07       ` Balaji Rao
  2008-03-09  5:06         ` Greg KH
  2008-03-09  6:33         ` Greg KH
  1 sibling, 2 replies; 18+ messages in thread
From: Balaji Rao @ 2008-03-08 22:07 UTC (permalink / raw)
  To: Greg KH, Mikael Pettersson; +Cc: kvm-devel, linux-kernel

On Thursday 06 March 2008 11:35:59 pm Greg KH wrote:
> On Thu, Mar 06, 2008 at 11:20:50PM +0530, Balaji Rao wrote:
> > On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
> > <snip>
> > > Where exactly in the code does that happen?  kobjects should not be
> > > "reused" as that implies that they are static, and not dynamically
> > > allocated, right?
> > >
> > > Which kobject is this?
> > Yes, its static. Here's the code from virt/kvm_main.c:1269
> > 
> > static struct sys_device kvm_sysdev = {
> >         .id = 0,
> >         .cls = &kvm_sysdev_class,
> > };
> > 
> > this sys_device is being registered/unregistered when kvm-intel is 
> > loaded/unloaded.
> 
> Ah, ok.  I'll add this patch then.
> 
> > > Ugh, is this the sys_device stuff?  I hate that code...
> > >
> > Yes it is! But, why do you hate it ?
> 
> For reasons like this :)
> 
> kobjects should not be static.  the sysdevice stuff was a hack when it
> was originally created and never touched since the mid 2.5 days.  It
> needs to be fixed up a lot, and is on my TODO list, slowly getting
> closer to the top...
Hi,

This patch does not fix it all! The problem is in fact more involved. I also get these BUG reports when I reload kvm-intel.

BUG kmalloc-8: Object already free
[  74.696570] -----------------------------------------------------------------------------
[   74.696596] 
[   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
[   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
[   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
[   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578

This happens because, sysdev_class_register assigns a name to the kobject, and kfrees the old name if any. The 
poisoned 'name' object persists in case of statically allocated kobjects and as its passed to kfree again when re 
registered, we get the above warning.

So, AFAICS the best way to solve this is by fixing the kobject users (kvm, oprofile etc.) to use dynamic kobjects instead 
of static ones or memset the kobject to zero before passing it to sysdev_register.

Greg ?

-- 
regards,
balaji rao
3rd year,
Dept. of Mechanical Engineering,
National Institute of Technology, Karnataka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-08 22:07       ` Balaji Rao
@ 2008-03-09  5:06         ` Greg KH
  2008-03-09  6:16           ` Greg KH
  2008-03-09  6:17           ` Balaji Rao
  2008-03-09  6:33         ` Greg KH
  1 sibling, 2 replies; 18+ messages in thread
From: Greg KH @ 2008-03-09  5:06 UTC (permalink / raw)
  To: Balaji Rao; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Sun, Mar 09, 2008 at 03:37:16AM +0530, Balaji Rao wrote:
> On Thursday 06 March 2008 11:35:59 pm Greg KH wrote:
> > On Thu, Mar 06, 2008 at 11:20:50PM +0530, Balaji Rao wrote:
> > > On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
> > > <snip>
> > > > Where exactly in the code does that happen?  kobjects should not be
> > > > "reused" as that implies that they are static, and not dynamically
> > > > allocated, right?
> > > >
> > > > Which kobject is this?
> > > Yes, its static. Here's the code from virt/kvm_main.c:1269
> > > 
> > > static struct sys_device kvm_sysdev = {
> > >         .id = 0,
> > >         .cls = &kvm_sysdev_class,
> > > };
> > > 
> > > this sys_device is being registered/unregistered when kvm-intel is 
> > > loaded/unloaded.
> > 
> > Ah, ok.  I'll add this patch then.
> > 
> > > > Ugh, is this the sys_device stuff?  I hate that code...
> > > >
> > > Yes it is! But, why do you hate it ?
> > 
> > For reasons like this :)
> > 
> > kobjects should not be static.  the sysdevice stuff was a hack when it
> > was originally created and never touched since the mid 2.5 days.  It
> > needs to be fixed up a lot, and is on my TODO list, slowly getting
> > closer to the top...
> Hi,
> 
> This patch does not fix it all! The problem is in fact more involved.
> I also get these BUG reports when I reload kvm-intel.
> 
> BUG kmalloc-8: Object already free
> [  74.696570] -----------------------------------------------------------------------------
> [   74.696596] 
> [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> 
> This happens because, sysdev_class_register assigns a name to the
> kobject, and kfrees the old name if any. The poisoned 'name' object
> persists in case of statically allocated kobjects and as its passed to
> kfree again when re registered, we get the above warning.

Ugh, that's horrible.  And people wonder why I hate the sysdev code :)

Does it also mean that when we do cleanup sysdev devices, we are freeing
a name that might not have been dynamically allocated?  If so, we need
to fix that as well.

A simple 'strdup' of the class name in sysdev_class_register() should
fix all of this, right?

> So, AFAICS the best way to solve this is by fixing the kobject users
> (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> memset the kobject to zero before passing it to sysdev_register.

Yes, that would be great to fix up, but probably unlikly so late in the
release cycle.

How about the patch below, does it work for you? (build tested only)

thanks,

greg k-h


---
 drivers/base/sys.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

--- a/drivers/base/sys.c
+++ b/drivers/base/sys.c
@@ -130,13 +130,16 @@ static struct kset *system_kset;
 
 int sysdev_class_register(struct sysdev_class * cls)
 {
+	char *name;
+
 	pr_debug("Registering sysdev class '%s'\n",
 		 kobject_name(&cls->kset.kobj));
 	INIT_LIST_HEAD(&cls->drivers);
 	cls->kset.kobj.parent = &system_kset->kobj;
 	cls->kset.kobj.ktype = &ktype_sysdev_class;
 	cls->kset.kobj.kset = system_kset;
-	kobject_set_name(&cls->kset.kobj, cls->name);
+	name = kstrdup(cls->name, GFP_KERNEL);
+	kobject_set_name(&cls->kset.kobj, name);
 	return kset_register(&cls->kset);
 }
 

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-09  5:06         ` Greg KH
@ 2008-03-09  6:16           ` Greg KH
  2008-03-09  6:17           ` Balaji Rao
  1 sibling, 0 replies; 18+ messages in thread
From: Greg KH @ 2008-03-09  6:16 UTC (permalink / raw)
  To: Balaji Rao; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Sat, Mar 08, 2008 at 09:06:49PM -0800, Greg KH wrote:
> On Sun, Mar 09, 2008 at 03:37:16AM +0530, Balaji Rao wrote:
> > On Thursday 06 March 2008 11:35:59 pm Greg KH wrote:
> > > On Thu, Mar 06, 2008 at 11:20:50PM +0530, Balaji Rao wrote:
> > > > On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
> > > > <snip>
> > > > > Where exactly in the code does that happen?  kobjects should not be
> > > > > "reused" as that implies that they are static, and not dynamically
> > > > > allocated, right?
> > > > >
> > > > > Which kobject is this?
> > > > Yes, its static. Here's the code from virt/kvm_main.c:1269
> > > > 
> > > > static struct sys_device kvm_sysdev = {
> > > >         .id = 0,
> > > >         .cls = &kvm_sysdev_class,
> > > > };
> > > > 
> > > > this sys_device is being registered/unregistered when kvm-intel is 
> > > > loaded/unloaded.
> > > 
> > > Ah, ok.  I'll add this patch then.
> > > 
> > > > > Ugh, is this the sys_device stuff?  I hate that code...
> > > > >
> > > > Yes it is! But, why do you hate it ?
> > > 
> > > For reasons like this :)
> > > 
> > > kobjects should not be static.  the sysdevice stuff was a hack when it
> > > was originally created and never touched since the mid 2.5 days.  It
> > > needs to be fixed up a lot, and is on my TODO list, slowly getting
> > > closer to the top...
> > Hi,
> > 
> > This patch does not fix it all! The problem is in fact more involved.
> > I also get these BUG reports when I reload kvm-intel.
> > 
> > BUG kmalloc-8: Object already free
> > [  74.696570] -----------------------------------------------------------------------------
> > [   74.696596] 
> > [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> > [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> > [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> > [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> > 
> > This happens because, sysdev_class_register assigns a name to the
> > kobject, and kfrees the old name if any. The poisoned 'name' object
> > persists in case of statically allocated kobjects and as its passed to
> > kfree again when re registered, we get the above warning.
> 
> Ugh, that's horrible.  And people wonder why I hate the sysdev code :)
> 
> Does it also mean that when we do cleanup sysdev devices, we are freeing
> a name that might not have been dynamically allocated?  If so, we need
> to fix that as well.
> 
> A simple 'strdup' of the class name in sysdev_class_register() should
> fix all of this, right?

Wait, no, that's not going to work, the name is already kstrduped.  That
patch shouldn't do anything except leak memory.

Let me try something else...

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-09  5:06         ` Greg KH
  2008-03-09  6:16           ` Greg KH
@ 2008-03-09  6:17           ` Balaji Rao
  1 sibling, 0 replies; 18+ messages in thread
From: Balaji Rao @ 2008-03-09  6:17 UTC (permalink / raw)
  To: Greg KH; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Sunday 09 March 2008 10:36:49 am Greg KH wrote:
> On Sun, Mar 09, 2008 at 03:37:16AM +0530, Balaji Rao wrote:
> > On Thursday 06 March 2008 11:35:59 pm Greg KH wrote:
> > > On Thu, Mar 06, 2008 at 11:20:50PM +0530, Balaji Rao wrote:
> > > > On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
> > > > <snip>
> > > > > Where exactly in the code does that happen?  kobjects should not be
> > > > > "reused" as that implies that they are static, and not dynamically
> > > > > allocated, right?
> > > > >
> > > > > Which kobject is this?
> > > > Yes, its static. Here's the code from virt/kvm_main.c:1269
> > > > 
> > > > static struct sys_device kvm_sysdev = {
> > > >         .id = 0,
> > > >         .cls = &kvm_sysdev_class,
> > > > };
> > > > 
> > > > this sys_device is being registered/unregistered when kvm-intel is 
> > > > loaded/unloaded.
> > > 
> > > Ah, ok.  I'll add this patch then.
> > > 
> > > > > Ugh, is this the sys_device stuff?  I hate that code...
> > > > >
> > > > Yes it is! But, why do you hate it ?
> > > 
> > > For reasons like this :)
> > > 
> > > kobjects should not be static.  the sysdevice stuff was a hack when it
> > > was originally created and never touched since the mid 2.5 days.  It
> > > needs to be fixed up a lot, and is on my TODO list, slowly getting
> > > closer to the top...
> > Hi,
> > 
> > This patch does not fix it all! The problem is in fact more involved.
> > I also get these BUG reports when I reload kvm-intel.
> > 
> > BUG kmalloc-8: Object already free
> > [  74.696570] -----------------------------------------------------------------------------
> > [   74.696596] 
> > [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> > [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> > [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> > [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> > 
> > This happens because, sysdev_class_register assigns a name to the
> > kobject, and kfrees the old name if any. The poisoned 'name' object
> > persists in case of statically allocated kobjects and as its passed to
> > kfree again when re registered, we get the above warning.
> 
> Ugh, that's horrible.  And people wonder why I hate the sysdev code :)
> 
> Does it also mean that when we do cleanup sysdev devices, we are freeing
> a name that might not have been dynamically allocated?  If so, we need
> to fix that as well.
> 
No, that doesn't happen. We free the name only if it is allocated..
> A simple 'strdup' of the class name in sysdev_class_register() should
> fix all of this, right?
> 
> > So, AFAICS the best way to solve this is by fixing the kobject users
> > (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> > memset the kobject to zero before passing it to sysdev_register.
> 
> Yes, that would be great to fix up, but probably unlikly so late in the
> release cycle.
> 
> How about the patch below, does it work for you? (build tested only)
> 
> thanks,
> 
> greg k-h
> 
> 
> ---
>  drivers/base/sys.c |    5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> --- a/drivers/base/sys.c
> +++ b/drivers/base/sys.c
> @@ -130,13 +130,16 @@ static struct kset *system_kset;
>  
>  int sysdev_class_register(struct sysdev_class * cls)
>  {
> +	char *name;
> +
>  	pr_debug("Registering sysdev class '%s'\n",
>  		 kobject_name(&cls->kset.kobj));
>  	INIT_LIST_HEAD(&cls->drivers);
>  	cls->kset.kobj.parent = &system_kset->kobj;
>  	cls->kset.kobj.ktype = &ktype_sysdev_class;
>  	cls->kset.kobj.kset = system_kset;
> -	kobject_set_name(&cls->kset.kobj, cls->name);
> +	name = kstrdup(cls->name, GFP_KERNEL);
> +	kobject_set_name(&cls->kset.kobj, name);
>  	return kset_register(&cls->kset);
>  }
>  
> 



-- 
regards,
balaji rao
3rd year,
Dept. of Mechanical Engineering,
National Institute of Technology, Karnataka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-08 22:07       ` Balaji Rao
  2008-03-09  5:06         ` Greg KH
@ 2008-03-09  6:33         ` Greg KH
  2008-03-09  6:36           ` Balaji Rao
  1 sibling, 1 reply; 18+ messages in thread
From: Greg KH @ 2008-03-09  6:33 UTC (permalink / raw)
  To: Balaji Rao; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Sun, Mar 09, 2008 at 03:37:16AM +0530, Balaji Rao wrote:
> On Thursday 06 March 2008 11:35:59 pm Greg KH wrote:
> > On Thu, Mar 06, 2008 at 11:20:50PM +0530, Balaji Rao wrote:
> > > On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
> > > <snip>
> > > > Where exactly in the code does that happen?  kobjects should not be
> > > > "reused" as that implies that they are static, and not dynamically
> > > > allocated, right?
> > > >
> > > > Which kobject is this?
> > > Yes, its static. Here's the code from virt/kvm_main.c:1269
> > > 
> > > static struct sys_device kvm_sysdev = {
> > >         .id = 0,
> > >         .cls = &kvm_sysdev_class,
> > > };
> > > 
> > > this sys_device is being registered/unregistered when kvm-intel is 
> > > loaded/unloaded.
> > 
> > Ah, ok.  I'll add this patch then.
> > 
> > > > Ugh, is this the sys_device stuff?  I hate that code...
> > > >
> > > Yes it is! But, why do you hate it ?
> > 
> > For reasons like this :)
> > 
> > kobjects should not be static.  the sysdevice stuff was a hack when it
> > was originally created and never touched since the mid 2.5 days.  It
> > needs to be fixed up a lot, and is on my TODO list, slowly getting
> > closer to the top...
> Hi,
> 
> This patch does not fix it all! The problem is in fact more involved. I also get these BUG reports when I reload kvm-intel.
> 
> BUG kmalloc-8: Object already free
> [  74.696570] -----------------------------------------------------------------------------
> [   74.696596] 
> [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> 
> This happens because, sysdev_class_register assigns a name to the
> kobject, and kfrees the old name if any. The poisoned 'name' object
> persists in case of statically allocated kobjects and as its passed to
> kfree again when re registered, we get the above warning.
> 
> So, AFAICS the best way to solve this is by fixing the kobject users
> (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> memset the kobject to zero before passing it to sysdev_register.

I like the memset idea, how about this patch instead?

thanks,

greg k-h

--- a/drivers/base/sys.c
+++ b/drivers/base/sys.c
@@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_
 	pr_debug("Registering sysdev class '%s'\n",
 		 kobject_name(&cls->kset.kobj));
 	INIT_LIST_HEAD(&cls->drivers);
+	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
 	cls->kset.kobj.parent = &system_kset->kobj;
 	cls->kset.kobj.ktype = &ktype_sysdev_class;
 	cls->kset.kobj.kset = system_kset;

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-09  6:33         ` Greg KH
@ 2008-03-09  6:36           ` Balaji Rao
  2008-03-09  7:03             ` Greg KH
  0 siblings, 1 reply; 18+ messages in thread
From: Balaji Rao @ 2008-03-09  6:36 UTC (permalink / raw)
  To: Greg KH; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Sunday 09 March 2008 12:03:08 pm Greg KH wrote:
> On Sun, Mar 09, 2008 at 03:37:16AM +0530, Balaji Rao wrote:
> > On Thursday 06 March 2008 11:35:59 pm Greg KH wrote:
> > > On Thu, Mar 06, 2008 at 11:20:50PM +0530, Balaji Rao wrote:
> > > > On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
> > > > <snip>
> > > > > Where exactly in the code does that happen?  kobjects should not be
> > > > > "reused" as that implies that they are static, and not dynamically
> > > > > allocated, right?
> > > > >
> > > > > Which kobject is this?
> > > > Yes, its static. Here's the code from virt/kvm_main.c:1269
> > > > 
> > > > static struct sys_device kvm_sysdev = {
> > > >         .id = 0,
> > > >         .cls = &kvm_sysdev_class,
> > > > };
> > > > 
> > > > this sys_device is being registered/unregistered when kvm-intel is 
> > > > loaded/unloaded.
> > > 
> > > Ah, ok.  I'll add this patch then.
> > > 
> > > > > Ugh, is this the sys_device stuff?  I hate that code...
> > > > >
> > > > Yes it is! But, why do you hate it ?
> > > 
> > > For reasons like this :)
> > > 
> > > kobjects should not be static.  the sysdevice stuff was a hack when it
> > > was originally created and never touched since the mid 2.5 days.  It
> > > needs to be fixed up a lot, and is on my TODO list, slowly getting
> > > closer to the top...
> > Hi,
> > 
> > This patch does not fix it all! The problem is in fact more involved. I also get these BUG reports when I reload 
kvm-intel.
> > 
> > BUG kmalloc-8: Object already free
> > [  74.696570] -----------------------------------------------------------------------------
> > [   74.696596] 
> > [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> > [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> > [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> > [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> > 
> > This happens because, sysdev_class_register assigns a name to the
> > kobject, and kfrees the old name if any. The poisoned 'name' object
> > persists in case of statically allocated kobjects and as its passed to
> > kfree again when re registered, we get the above warning.
> > 
> > So, AFAICS the best way to solve this is by fixing the kobject users
> > (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> > memset the kobject to zero before passing it to sysdev_register.
> 
> I like the memset idea, how about this patch instead?
> 
> thanks,
> 
> greg k-h
> 
> --- a/drivers/base/sys.c
> +++ b/drivers/base/sys.c
> @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_
>  	pr_debug("Registering sysdev class '%s'\n",
>  		 kobject_name(&cls->kset.kobj));
>  	INIT_LIST_HEAD(&cls->drivers);
> +	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
>  	cls->kset.kobj.parent = &system_kset->kobj;
>  	cls->kset.kobj.ktype = &ktype_sysdev_class;
>  	cls->kset.kobj.kset = system_kset;
> 

This should work.But I am afraid if we zeroed it, the kfree poison test wouldn't catch even the genuine cases. Isn't it ? 
CMIIW.

A better fix according to me would be to zero the kobject in the places where we know it is being re-registered (kvm, 
oprofile) etc. This should do for now. But we should fix the sys_device users later, for the next cycle.

-- 
regards,
balaji rao
3rd year,
Dept. of Mechanical Engineering,
National Institute of Technology, Karnataka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-09  6:36           ` Balaji Rao
@ 2008-03-09  7:03             ` Greg KH
  2008-03-09  7:21               ` Balaji Rao
  0 siblings, 1 reply; 18+ messages in thread
From: Greg KH @ 2008-03-09  7:03 UTC (permalink / raw)
  To: Balaji Rao; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Sun, Mar 09, 2008 at 12:06:02PM +0530, Balaji Rao wrote:
> On Sunday 09 March 2008 12:03:08 pm Greg KH wrote:
> > On Sun, Mar 09, 2008 at 03:37:16AM +0530, Balaji Rao wrote:
> > > On Thursday 06 March 2008 11:35:59 pm Greg KH wrote:
> > > > On Thu, Mar 06, 2008 at 11:20:50PM +0530, Balaji Rao wrote:
> > > > > On Thursday 06 March 2008 10:35:14 pm Greg KH wrote:
> > > > > <snip>
> > > > > > Where exactly in the code does that happen?  kobjects should not be
> > > > > > "reused" as that implies that they are static, and not dynamically
> > > > > > allocated, right?
> > > > > >
> > > > > > Which kobject is this?
> > > > > Yes, its static. Here's the code from virt/kvm_main.c:1269
> > > > > 
> > > > > static struct sys_device kvm_sysdev = {
> > > > >         .id = 0,
> > > > >         .cls = &kvm_sysdev_class,
> > > > > };
> > > > > 
> > > > > this sys_device is being registered/unregistered when kvm-intel is 
> > > > > loaded/unloaded.
> > > > 
> > > > Ah, ok.  I'll add this patch then.
> > > > 
> > > > > > Ugh, is this the sys_device stuff?  I hate that code...
> > > > > >
> > > > > Yes it is! But, why do you hate it ?
> > > > 
> > > > For reasons like this :)
> > > > 
> > > > kobjects should not be static.  the sysdevice stuff was a hack when it
> > > > was originally created and never touched since the mid 2.5 days.  It
> > > > needs to be fixed up a lot, and is on my TODO list, slowly getting
> > > > closer to the top...
> > > Hi,
> > > 
> > > This patch does not fix it all! The problem is in fact more involved. I also get these BUG reports when I reload 
> kvm-intel.
> > > 
> > > BUG kmalloc-8: Object already free
> > > [  74.696570] -----------------------------------------------------------------------------
> > > [   74.696596] 
> > > [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> > > [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> > > [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> > > [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> > > 
> > > This happens because, sysdev_class_register assigns a name to the
> > > kobject, and kfrees the old name if any. The poisoned 'name' object
> > > persists in case of statically allocated kobjects and as its passed to
> > > kfree again when re registered, we get the above warning.
> > > 
> > > So, AFAICS the best way to solve this is by fixing the kobject users
> > > (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> > > memset the kobject to zero before passing it to sysdev_register.
> > 
> > I like the memset idea, how about this patch instead?
> > 
> > thanks,
> > 
> > greg k-h
> > 
> > --- a/drivers/base/sys.c
> > +++ b/drivers/base/sys.c
> > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_
> >  	pr_debug("Registering sysdev class '%s'\n",
> >  		 kobject_name(&cls->kset.kobj));
> >  	INIT_LIST_HEAD(&cls->drivers);
> > +	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
> >  	cls->kset.kobj.parent = &system_kset->kobj;
> >  	cls->kset.kobj.ktype = &ktype_sysdev_class;
> >  	cls->kset.kobj.kset = system_kset;
> > 
> 
> This should work.But I am afraid if we zeroed it, the kfree poison
> test wouldn't catch even the genuine cases. Isn't it ?  CMIIW.

What "genuine cases"?  kobjects should always be initialized to zero
before they are registered.

> A better fix according to me would be to zero the kobject in the
> places where we know it is being re-registered (kvm, oprofile) etc.
> This should do for now. But we should fix the sys_device users later,
> for the next cycle.

Are you sure you know all of the sysdev_class objects that can be
re-registered?

Can you test this patch out?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-09  7:03             ` Greg KH
@ 2008-03-09  7:21               ` Balaji Rao
  2008-03-09 10:49                 ` Mikael Pettersson
  2008-03-10 15:52                 ` Greg KH
  0 siblings, 2 replies; 18+ messages in thread
From: Balaji Rao @ 2008-03-09  7:21 UTC (permalink / raw)
  To: Greg KH; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Sunday 09 March 2008 12:33:07 pm Greg KH wrote:
<snip>
> > > > Hi,
> > > > 
> > > > This patch does not fix it all! The problem is in fact more involved. I also get these BUG reports when I reload 
> > kvm-intel.
> > > > 
> > > > BUG kmalloc-8: Object already free
> > > > [  74.696570] -----------------------------------------------------------------------------
> > > > [   74.696596] 
> > > > [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> > > > [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> > > > [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> > > > [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> > > > 
> > > > This happens because, sysdev_class_register assigns a name to the
> > > > kobject, and kfrees the old name if any. The poisoned 'name' object
> > > > persists in case of statically allocated kobjects and as its passed to
> > > > kfree again when re registered, we get the above warning.
> > > > 
> > > > So, AFAICS the best way to solve this is by fixing the kobject users
> > > > (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> > > > memset the kobject to zero before passing it to sysdev_register.
> > > 
> > > I like the memset idea, how about this patch instead?
> > > 
> > > thanks,
> > > 
> > > greg k-h
> > > 
> > > --- a/drivers/base/sys.c
> > > +++ b/drivers/base/sys.c
> > > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_
> > >  	pr_debug("Registering sysdev class '%s'\n",
> > >  		 kobject_name(&cls->kset.kobj));
> > >  	INIT_LIST_HEAD(&cls->drivers);
> > > +	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
> > >  	cls->kset.kobj.parent = &system_kset->kobj;
> > >  	cls->kset.kobj.ktype = &ktype_sysdev_class;
> > >  	cls->kset.kobj.kset = system_kset;
> > > 
> > 
> > This should work.But I am afraid if we zeroed it, the kfree poison
> > test wouldn't catch even the genuine cases. Isn't it ?  CMIIW.
> 
> What "genuine cases"?  kobjects should always be initialized to zero
> before they are registered.
> 
The cases in which a freed kobject is passed to the register function ? Probably they will be caught when do a memset..
> > A better fix according to me would be to zero the kobject in the
> > places where we know it is being re-registered (kvm, oprofile) etc.
> > This should do for now. But we should fix the sys_device users later,
> > for the next cycle.
> 
> Are you sure you know all of the sysdev_class objects that can be
> re-registered?
> 
Hmm.. Right. No, I only know that KVM and Oprofile use it.
> Can you test this patch out?
> 
Yes the idea works. One more memset is needed in sysdev_register. Here's the final patch.

diff --git a/drivers/base/sys.c b/drivers/base/sys.c
index 2f79c55..7c839d9 100644
--- a/drivers/base/sys.c
+++ b/drivers/base/sys.c
@@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_class * cls)
        pr_debug("Registering sysdev class '%s'\n",
                 kobject_name(&cls->kset.kobj));
        INIT_LIST_HEAD(&cls->drivers);
+       memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
        cls->kset.kobj.parent = &system_kset->kobj;
        cls->kset.kobj.ktype = &ktype_sysdev_class;
        cls->kset.kobj.kset = system_kset;
@@ -227,6 +228,7 @@ int sysdev_register(struct sys_device * sysdev)
 
        pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
 
+       memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
        /* Make sure the kset is set */
        sysdev->kobj.kset = &cls->kset;

-- 
regards,
balaji rao
3rd year,
Dept. of Mechanical Engineering,
National Institute of Technology, Karnataka

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-09  7:21               ` Balaji Rao
@ 2008-03-09 10:49                 ` Mikael Pettersson
  2008-03-10 17:20                   ` Greg KH
  2008-03-10 15:52                 ` Greg KH
  1 sibling, 1 reply; 18+ messages in thread
From: Mikael Pettersson @ 2008-03-09 10:49 UTC (permalink / raw)
  To: Balaji Rao; +Cc: Greg KH, Mikael Pettersson, kvm-devel, linux-kernel

Balaji Rao writes:
 > Yes the idea works. One more memset is needed in sysdev_register. Here's the final patch.
 > 
 > diff --git a/drivers/base/sys.c b/drivers/base/sys.c
 > index 2f79c55..7c839d9 100644
 > --- a/drivers/base/sys.c
 > +++ b/drivers/base/sys.c
 > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_class * cls)
 >         pr_debug("Registering sysdev class '%s'\n",
 >                  kobject_name(&cls->kset.kobj));
 >         INIT_LIST_HEAD(&cls->drivers);
 > +       memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
 >         cls->kset.kobj.parent = &system_kset->kobj;
 >         cls->kset.kobj.ktype = &ktype_sysdev_class;
 >         cls->kset.kobj.kset = system_kset;
 > @@ -227,6 +228,7 @@ int sysdev_register(struct sys_device * sysdev)
 >  
 >         pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
 >  
 > +       memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
 >         /* Make sure the kset is set */
 >         sysdev->kobj.kset = &cls->kset;
 > 

Thanks, 2.6.25-rc4 + these two memset()s is finally stable for
me with no warnings, BUG()s, or panics.

(However, the patch is whitespace damaged with initial tabs
converted to spaces.)

If you want to pass this on to Linus, you can add a

Tested-by: Mikael Pettersson <mikpe@it.uu.se>

/Mikael

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-09  7:21               ` Balaji Rao
  2008-03-09 10:49                 ` Mikael Pettersson
@ 2008-03-10 15:52                 ` Greg KH
  2008-03-10 16:05                   ` Balaji Rao
  1 sibling, 1 reply; 18+ messages in thread
From: Greg KH @ 2008-03-10 15:52 UTC (permalink / raw)
  To: Balaji Rao; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Sun, Mar 09, 2008 at 12:51:15PM +0530, Balaji Rao wrote:
> On Sunday 09 March 2008 12:33:07 pm Greg KH wrote:
> <snip>
> > > > > Hi,
> > > > > 
> > > > > This patch does not fix it all! The problem is in fact more involved. I also get these BUG reports when I reload 
> > > kvm-intel.
> > > > > 
> > > > > BUG kmalloc-8: Object already free
> > > > > [  74.696570] -----------------------------------------------------------------------------
> > > > > [   74.696596] 
> > > > > [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> > > > > [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> > > > > [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> > > > > [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> > > > > 
> > > > > This happens because, sysdev_class_register assigns a name to the
> > > > > kobject, and kfrees the old name if any. The poisoned 'name' object
> > > > > persists in case of statically allocated kobjects and as its passed to
> > > > > kfree again when re registered, we get the above warning.
> > > > > 
> > > > > So, AFAICS the best way to solve this is by fixing the kobject users
> > > > > (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> > > > > memset the kobject to zero before passing it to sysdev_register.
> > > > 
> > > > I like the memset idea, how about this patch instead?
> > > > 
> > > > thanks,
> > > > 
> > > > greg k-h
> > > > 
> > > > --- a/drivers/base/sys.c
> > > > +++ b/drivers/base/sys.c
> > > > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_
> > > >  	pr_debug("Registering sysdev class '%s'\n",
> > > >  		 kobject_name(&cls->kset.kobj));
> > > >  	INIT_LIST_HEAD(&cls->drivers);
> > > > +	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
> > > >  	cls->kset.kobj.parent = &system_kset->kobj;
> > > >  	cls->kset.kobj.ktype = &ktype_sysdev_class;
> > > >  	cls->kset.kobj.kset = system_kset;
> > > > 
> > > 
> > > This should work.But I am afraid if we zeroed it, the kfree poison
> > > test wouldn't catch even the genuine cases. Isn't it ?  CMIIW.
> > 
> > What "genuine cases"?  kobjects should always be initialized to zero
> > before they are registered.
> > 
> The cases in which a freed kobject is passed to the register function ? Probably they will be caught when do a memset..
> > > A better fix according to me would be to zero the kobject in the
> > > places where we know it is being re-registered (kvm, oprofile) etc.
> > > This should do for now. But we should fix the sys_device users later,
> > > for the next cycle.
> > 
> > Are you sure you know all of the sysdev_class objects that can be
> > re-registered?
> > 
> Hmm.. Right. No, I only know that KVM and Oprofile use it.
> > Can you test this patch out?
> > 
> Yes the idea works. One more memset is needed in sysdev_register. Here's the final patch.
> 
> diff --git a/drivers/base/sys.c b/drivers/base/sys.c
> index 2f79c55..7c839d9 100644
> --- a/drivers/base/sys.c
> +++ b/drivers/base/sys.c
> @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_class * cls)
>         pr_debug("Registering sysdev class '%s'\n",
>                  kobject_name(&cls->kset.kobj));
>         INIT_LIST_HEAD(&cls->drivers);
> +       memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
>         cls->kset.kobj.parent = &system_kset->kobj;
>         cls->kset.kobj.ktype = &ktype_sysdev_class;
>         cls->kset.kobj.kset = system_kset;
> @@ -227,6 +228,7 @@ int sysdev_register(struct sys_device * sysdev)
>  
>         pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
>  
> +       memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
>         /* Make sure the kset is set */
>         sysdev->kobj.kset = &cls->kset;

Yes, you're right.

This also means I can drop the kobject_cleanup() patch from you as well,
as that should no longer be needed, right?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-10 15:52                 ` Greg KH
@ 2008-03-10 16:05                   ` Balaji Rao
  2008-03-10 17:20                     ` Greg KH
  0 siblings, 1 reply; 18+ messages in thread
From: Balaji Rao @ 2008-03-10 16:05 UTC (permalink / raw)
  To: Greg KH; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Monday 10 March 2008 09:22:00 pm Greg KH wrote:
> On Sun, Mar 09, 2008 at 12:51:15PM +0530, Balaji Rao wrote:
> > On Sunday 09 March 2008 12:33:07 pm Greg KH wrote:
> > <snip>
> > > > > > Hi,
> > > > > > 
> > > > > > This patch does not fix it all! The problem is in fact more involved. I also get these BUG reports when I 
reload 
> > > > kvm-intel.
> > > > > > 
> > > > > > BUG kmalloc-8: Object already free
> > > > > > [  74.696570] -----------------------------------------------------------------------------
> > > > > > [   74.696596] 
> > > > > > [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> > > > > > [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> > > > > > [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> > > > > > [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> > > > > > 
> > > > > > This happens because, sysdev_class_register assigns a name to the
> > > > > > kobject, and kfrees the old name if any. The poisoned 'name' object
> > > > > > persists in case of statically allocated kobjects and as its passed to
> > > > > > kfree again when re registered, we get the above warning.
> > > > > > 
> > > > > > So, AFAICS the best way to solve this is by fixing the kobject users
> > > > > > (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> > > > > > memset the kobject to zero before passing it to sysdev_register.
> > > > > 
> > > > > I like the memset idea, how about this patch instead?
> > > > > 
> > > > > thanks,
> > > > > 
> > > > > greg k-h
> > > > > 
> > > > > --- a/drivers/base/sys.c
> > > > > +++ b/drivers/base/sys.c
> > > > > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_
> > > > >  	pr_debug("Registering sysdev class '%s'\n",
> > > > >  		 kobject_name(&cls->kset.kobj));
> > > > >  	INIT_LIST_HEAD(&cls->drivers);
> > > > > +	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
> > > > >  	cls->kset.kobj.parent = &system_kset->kobj;
> > > > >  	cls->kset.kobj.ktype = &ktype_sysdev_class;
> > > > >  	cls->kset.kobj.kset = system_kset;
> > > > > 
> > > > 
> > > > This should work.But I am afraid if we zeroed it, the kfree poison
> > > > test wouldn't catch even the genuine cases. Isn't it ?  CMIIW.
> > > 
> > > What "genuine cases"?  kobjects should always be initialized to zero
> > > before they are registered.
> > > 
> > The cases in which a freed kobject is passed to the register function ? Probably they will be caught when do a memset..
> > > > A better fix according to me would be to zero the kobject in the
> > > > places where we know it is being re-registered (kvm, oprofile) etc.
> > > > This should do for now. But we should fix the sys_device users later,
> > > > for the next cycle.
> > > 
> > > Are you sure you know all of the sysdev_class objects that can be
> > > re-registered?
> > > 
> > Hmm.. Right. No, I only know that KVM and Oprofile use it.
> > > Can you test this patch out?
> > > 
> > Yes the idea works. One more memset is needed in sysdev_register. Here's the final patch.
> > 
> > diff --git a/drivers/base/sys.c b/drivers/base/sys.c
> > index 2f79c55..7c839d9 100644
> > --- a/drivers/base/sys.c
> > +++ b/drivers/base/sys.c
> > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_class * cls)
> >         pr_debug("Registering sysdev class '%s'\n",
> >                  kobject_name(&cls->kset.kobj));
> >         INIT_LIST_HEAD(&cls->drivers);
> > +       memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
> >         cls->kset.kobj.parent = &system_kset->kobj;
> >         cls->kset.kobj.ktype = &ktype_sysdev_class;
> >         cls->kset.kobj.kset = system_kset;
> > @@ -227,6 +228,7 @@ int sysdev_register(struct sys_device * sysdev)
> >  
> >         pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
> >  
> > +       memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
> >         /* Make sure the kset is set */
> >         sysdev->kobj.kset = &cls->kset;
> 
> Yes, you're right.
> 
> This also means I can drop the kobject_cleanup() patch from you as well,
> as that should no longer be needed, right?
Yes, That's no longer needed. 

I am resending the patch, as the previous one had some whitespace issues.

Signed-off-by: Balaji Rao <balajirrao@gmail.com>
Tested-by: Mikael Pettersson <mikpe@it.uu.se>

diff --git a/drivers/base/sys.c b/drivers/base/sys.c
index 2f79c55..7c839d9 100644
--- a/drivers/base/sys.c
+++ b/drivers/base/sys.c
@@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_class * cls)
 	pr_debug("Registering sysdev class '%s'\n",
 		 kobject_name(&cls->kset.kobj));
 	INIT_LIST_HEAD(&cls->drivers);
+	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
 	cls->kset.kobj.parent = &system_kset->kobj;
 	cls->kset.kobj.ktype = &ktype_sysdev_class;
 	cls->kset.kobj.kset = system_kset;
@@ -227,6 +228,7 @@ int sysdev_register(struct sys_device * sysdev)
 
 	pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
 
+	memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
 	/* Make sure the kset is set */
 	sysdev->kobj.kset = &cls->kset;
 

-- 
regards,
balaji rao
3rd year,
Dept. of Mechanical Engineering,
National Institute of Technology, Karnataka

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-09 10:49                 ` Mikael Pettersson
@ 2008-03-10 17:20                   ` Greg KH
  0 siblings, 0 replies; 18+ messages in thread
From: Greg KH @ 2008-03-10 17:20 UTC (permalink / raw)
  To: Mikael Pettersson; +Cc: Balaji Rao, kvm-devel, linux-kernel

On Sun, Mar 09, 2008 at 11:49:26AM +0100, Mikael Pettersson wrote:
> Balaji Rao writes:
>  > Yes the idea works. One more memset is needed in sysdev_register. Here's the final patch.
>  > 
>  > diff --git a/drivers/base/sys.c b/drivers/base/sys.c
>  > index 2f79c55..7c839d9 100644
>  > --- a/drivers/base/sys.c
>  > +++ b/drivers/base/sys.c
>  > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_class * cls)
>  >         pr_debug("Registering sysdev class '%s'\n",
>  >                  kobject_name(&cls->kset.kobj));
>  >         INIT_LIST_HEAD(&cls->drivers);
>  > +       memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
>  >         cls->kset.kobj.parent = &system_kset->kobj;
>  >         cls->kset.kobj.ktype = &ktype_sysdev_class;
>  >         cls->kset.kobj.kset = system_kset;
>  > @@ -227,6 +228,7 @@ int sysdev_register(struct sys_device * sysdev)
>  >  
>  >         pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
>  >  
>  > +       memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
>  >         /* Make sure the kset is set */
>  >         sysdev->kobj.kset = &cls->kset;
>  > 
> 
> Thanks, 2.6.25-rc4 + these two memset()s is finally stable for
> me with no warnings, BUG()s, or panics.
> 
> (However, the patch is whitespace damaged with initial tabs
> converted to spaces.)
> 
> If you want to pass this on to Linus, you can add a
> 
> Tested-by: Mikael Pettersson <mikpe@it.uu.se>

Thanks for testing.

greg k-h

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] Mark kobjects as unitialized
  2008-03-10 16:05                   ` Balaji Rao
@ 2008-03-10 17:20                     ` Greg KH
  0 siblings, 0 replies; 18+ messages in thread
From: Greg KH @ 2008-03-10 17:20 UTC (permalink / raw)
  To: Balaji Rao; +Cc: Mikael Pettersson, kvm-devel, linux-kernel

On Mon, Mar 10, 2008 at 09:35:56PM +0530, Balaji Rao wrote:
> On Monday 10 March 2008 09:22:00 pm Greg KH wrote:
> > On Sun, Mar 09, 2008 at 12:51:15PM +0530, Balaji Rao wrote:
> > > On Sunday 09 March 2008 12:33:07 pm Greg KH wrote:
> > > <snip>
> > > > > > > Hi,
> > > > > > > 
> > > > > > > This patch does not fix it all! The problem is in fact more involved. I also get these BUG reports when I 
> reload 
> > > > > kvm-intel.
> > > > > > > 
> > > > > > > BUG kmalloc-8: Object already free
> > > > > > > [  74.696570] -----------------------------------------------------------------------------
> > > > > > > [   74.696596] 
> > > > > > > [   74.697310] INFO: Allocated in strndup_user+0x30/0x62 age=587 cpu=2 pid=1439
> > > > > > > [   74.697845] INFO: Freed in kobject_set_name_vargs+0x29/0x32 age=559 cpu=3 pid=1439
> > > > > > > [   74.698008] INFO: Slab 0xc16f93a0 used=10 fp=0xf7c9d2d8 flags=0x10000c3
> > > > > > > [   74.698008] INFO: Object 0xf7c9d1f8 @offset=504 fp=0xf7c9d578
> > > > > > > 
> > > > > > > This happens because, sysdev_class_register assigns a name to the
> > > > > > > kobject, and kfrees the old name if any. The poisoned 'name' object
> > > > > > > persists in case of statically allocated kobjects and as its passed to
> > > > > > > kfree again when re registered, we get the above warning.
> > > > > > > 
> > > > > > > So, AFAICS the best way to solve this is by fixing the kobject users
> > > > > > > (kvm, oprofile etc.) to use dynamic kobjects instead of static ones or
> > > > > > > memset the kobject to zero before passing it to sysdev_register.
> > > > > > 
> > > > > > I like the memset idea, how about this patch instead?
> > > > > > 
> > > > > > thanks,
> > > > > > 
> > > > > > greg k-h
> > > > > > 
> > > > > > --- a/drivers/base/sys.c
> > > > > > +++ b/drivers/base/sys.c
> > > > > > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_
> > > > > >  	pr_debug("Registering sysdev class '%s'\n",
> > > > > >  		 kobject_name(&cls->kset.kobj));
> > > > > >  	INIT_LIST_HEAD(&cls->drivers);
> > > > > > +	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
> > > > > >  	cls->kset.kobj.parent = &system_kset->kobj;
> > > > > >  	cls->kset.kobj.ktype = &ktype_sysdev_class;
> > > > > >  	cls->kset.kobj.kset = system_kset;
> > > > > > 
> > > > > 
> > > > > This should work.But I am afraid if we zeroed it, the kfree poison
> > > > > test wouldn't catch even the genuine cases. Isn't it ?  CMIIW.
> > > > 
> > > > What "genuine cases"?  kobjects should always be initialized to zero
> > > > before they are registered.
> > > > 
> > > The cases in which a freed kobject is passed to the register function ? Probably they will be caught when do a memset..
> > > > > A better fix according to me would be to zero the kobject in the
> > > > > places where we know it is being re-registered (kvm, oprofile) etc.
> > > > > This should do for now. But we should fix the sys_device users later,
> > > > > for the next cycle.
> > > > 
> > > > Are you sure you know all of the sysdev_class objects that can be
> > > > re-registered?
> > > > 
> > > Hmm.. Right. No, I only know that KVM and Oprofile use it.
> > > > Can you test this patch out?
> > > > 
> > > Yes the idea works. One more memset is needed in sysdev_register. Here's the final patch.
> > > 
> > > diff --git a/drivers/base/sys.c b/drivers/base/sys.c
> > > index 2f79c55..7c839d9 100644
> > > --- a/drivers/base/sys.c
> > > +++ b/drivers/base/sys.c
> > > @@ -133,6 +133,7 @@ int sysdev_class_register(struct sysdev_class * cls)
> > >         pr_debug("Registering sysdev class '%s'\n",
> > >                  kobject_name(&cls->kset.kobj));
> > >         INIT_LIST_HEAD(&cls->drivers);
> > > +       memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
> > >         cls->kset.kobj.parent = &system_kset->kobj;
> > >         cls->kset.kobj.ktype = &ktype_sysdev_class;
> > >         cls->kset.kobj.kset = system_kset;
> > > @@ -227,6 +228,7 @@ int sysdev_register(struct sys_device * sysdev)
> > >  
> > >         pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
> > >  
> > > +       memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
> > >         /* Make sure the kset is set */
> > >         sysdev->kobj.kset = &cls->kset;
> > 
> > Yes, you're right.
> > 
> > This also means I can drop the kobject_cleanup() patch from you as well,
> > as that should no longer be needed, right?
> Yes, That's no longer needed. 
> 
> I am resending the patch, as the previous one had some whitespace issues.

I think that gmail did that, my original version was fine :)

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2008-03-10 17:24 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-06 16:53 [PATCH] Mark kobjects as unitialized Balaji Rao
2008-03-06 17:05 ` Greg KH
2008-03-06 17:50   ` Balaji Rao
2008-03-06 18:05     ` Greg KH
2008-03-07  8:56       ` [kvm-devel] " Avi Kivity
2008-03-08 22:07       ` Balaji Rao
2008-03-09  5:06         ` Greg KH
2008-03-09  6:16           ` Greg KH
2008-03-09  6:17           ` Balaji Rao
2008-03-09  6:33         ` Greg KH
2008-03-09  6:36           ` Balaji Rao
2008-03-09  7:03             ` Greg KH
2008-03-09  7:21               ` Balaji Rao
2008-03-09 10:49                 ` Mikael Pettersson
2008-03-10 17:20                   ` Greg KH
2008-03-10 15:52                 ` Greg KH
2008-03-10 16:05                   ` Balaji Rao
2008-03-10 17:20                     ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).