Re: [-mm3 PATCH] (Retry) Check the return value of kobject_add and etc.

From: WANG Cong
Date: Thu Apr 05 2007 - 11:25:01 EST


On Thu, Apr 05, 2007 at 05:05:14PM +0200, Cornelia Huck wrote:
>On Thu, 5 Apr 2007 22:44:09 +0800,
>WANG Cong <xiyou.wangcong@xxxxxxxxx> wrote:
>
>> On Thu, Apr 05, 2007 at 11:11:42AM +0200, Cornelia Huck wrote:
>> >On Thu, 5 Apr 2007 12:54:11 +0800,
>> >WANG Cong <xiyou.wangcong@xxxxxxxxx> wrote:
>> >
>> >> --- linux-2.6.21-rc5-mm3/fs/partitions/check.c.orig 2007-03-30 21:35:45.000000000 +0800
>> >> +++ linux-2.6.21-rc5-mm3/fs/partitions/check.c 2007-04-02 21:29:02.000000000 +0800
>> >> @@ -385,10 +385,18 @@ void add_partition(struct gendisk *disk,
>> >> p->kobj.parent = &disk->kobj;
>> >> p->kobj.ktype = &ktype_part;
>> >> kobject_init(&p->kobj);
>> >> - kobject_add(&p->kobj);
>> >> + if (kobject_add(&p->kobj)) {
>> >> + kfree(p);
>> >> + return;
>> >> + }
>> >> if (!disk->part_uevent_suppress)
>> >> kobject_uevent(&p->kobj, KOBJ_ADD);
>> >> - sysfs_create_link(&p->kobj, &block_subsys.kset.kobj, "subsystem");
>> >> + if (sysfs_create_link(&p->kobj, &block_subsys.kset.kobj, "subsystem")) {
>> >> + kobject_uevent(&p->kobj, KOBJ_REMOVE);
>> >> + kobject_del(&p->kobj);
>> >> + kfree(p);
>> >
>> >You should use kobject_put instead of kfree, since someone could have
>> >(theoretically) obtained a reference on the object after the
>> >kobject_add. (Or just use kobject_unregister, if the delete uevent
>> >isn't suppressed anyway.)
>> >
>>
>> I can't understand that. The memory pointed by 'p' is obtained via kmalloc(), _not_ kobject_get. How can we use kobject_put instead?
>
>The hd_struct *p embeds a kobject kobj (which, amongst other things,
>provides reference counting for the hd_struct). The release function
>for the kobject will free the embedding object (note that kobject_init
>sets the reference count to 1). That means, if kobject_put(&p->kobj)
>will drop the reference to 0, the release function (part_release) will
>free p. (See also Documentation/kobject.txt.)

Thank you very much! I know. So I should replace all kfree with kobject_put, like this one:

- sysfs_create_link(&p->kobj, &block_subsys.kset.kobj, "subsystem");
+ if (sysfs_create_link(&p->kobj, &block_subsys.kset.kobj, "subsystem")) {
+ kobject_uevent(&p->kobj, KOBJ_REMOVE);
+ kobject_del(&p->kobj);
+ kobject_put(&p->kobj);
+ return;
+ }

Is that all right?

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/