Skip to contentSkip to conversation

pktcdvd : add kobject_put when kobject register fails

PATCH In review Dave Young 2 participants 5 messages Dec 4, 2007 Block layer

lore
Share & tools
lore

Dave Young

author
#0 lore raw

PATCH 1/1 pktcdvd : add kobject_put when kobject register fails

Kobject_put should be called when kobject register functioin fails, so the the kobj ref count touch zero and then the proper cleanup routines will be called.

---
drivers/block/pktcdvd.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
drivers/block/pktcdvd.c +3 -1
diff -upr linux/drivers/block/pktcdvd.c linux.new/drivers/block/pktcdvd.c
@@ -117,8 +117,10 @@ static struct pktcdvd_kobj* pkt_kobj_cre
 	p->kobj.parent = parent;
 	p->kobj.ktype = ktype;
 	p->pd = pd;
-	if (kobject_register(&p->kobj) != 0)
+	if (kobject_register(&p->kobj) != 0) {
+		kobject_put(&p->kobj);
 		return NULL;
+	}
 	return p;
 }
 /*
#1 lore raw

Hi Dave,

Quoted 3 lines · Dave Young

Kobject_put should be called when kobject register functioin fails, so the the kobj ref count touch zero and then the proper cleanup routines will be called.

[snip]

drivers/block/pktcdvd.c 2007-11-30 13:24:08.000000000 +0800 static struct pktcdvd_kobj* pkt_kobj_cre · line 122

Quoted 11 lines

diff -upr linux/drivers/block/pktcdvd.c linux.new/drivers/block/pktcdvd.c

drivers/block/pktcdvd.c 2007-11-30 13:24:08.000000000 +0800 renamed from drivers/block/pktcdvd.c 2007-11-30 13:13:44.000000000 +0800 +2 -1
@@ -117,8 +117,10 @@ static struct pktcdvd_kobj* pkt_kobj_cre
        p->kobj.parent = parent;
        p->kobj.ktype = ktype;
        p->pd = pd;
-       if (kobject_register(&p->kobj) != 0)
+       if (kobject_register(&p->kobj) != 0) {
+               kobject_put(&p->kobj);
                return NULL;

This looks wrong to me. AFAICT the only thing that can fail kobject_register() is kobject_add() and it cleans up after itself. Am I missing something here?

Pekka

author
#2 lore raw
Quoted 9 lines · Pekka Enberg

Hi Dave,

Quoted 3 lines · Dave Young

Kobject_put should be called when kobject register functioin fails, so the the kobj ref count touch zero and then the proper cleanup routines will be called.

[snip]

Quoted 15 lines

drivers/block/pktcdvd.c 2007-11-30 13:24:08.000000000 +0800 static struct pktcdvd_kobj* pkt_kobj_cre · line 122

Quoted 11 lines

diff -upr linux/drivers/block/pktcdvd.c linux.new/drivers/block/pktcdvd.c

drivers/block/pktcdvd.c 2007-11-30 13:24:08.000000000 +0800 renamed from drivers/block/pktcdvd.c 2007-11-30 13:13:44.000000000 +0800 +2 -1
@@ -117,8 +117,10 @@ static struct pktcdvd_kobj* pkt_kobj_cre
        p->kobj.parent = parent;
        p->kobj.ktype = ktype;
        p->pd = pd;
-       if (kobject_register(&p->kobj) != 0)
+       if (kobject_register(&p->kobj) != 0) {
+               kobject_put(&p->kobj);
                return NULL;

This looks wrong to me. AFAICT the only thing that can fail kobject_register() is kobject_add() and it cleans up after itself. Am I missing something here?

Hi,
the kobject reference is get in kobject_init, and then kobject_add, if kobject_add fail, it will only cleanup the reference got by itsself.

This topic is discussed in:
http://lkml.org/lkml/2007/11/30/9

Regards
dave

Quoted 3 lines

Pekka

author Reviewed-by Pekka Enberg
#4 lore raw
Quoted 11 lines · Pekka J Enberg

Hi Dave,

Quoted 5 lines · Dave Young

the kobject reference is get in kobject_init, and then kobject_add, if kobject_add fail, it will only cleanup the reference got by itsself.

This topic is discussed in:
http://lkml.org/lkml/2007/11/30/9

Ok, thanks! Please the explanation to the changelog too.

Ok, lets add some explanation.
------------------------------

kobject_put should be called when kobject register functioin fails.

In kobject_register, the kobject reference is get in kobject_init, and then kobject_add. If kobject_add fail, it will only cleanup the reference got by itself.

---
drivers/block/pktcdvd.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
drivers/block/pktcdvd.c +3 -1
diff -upr linux/drivers/block/pktcdvd.c linux.new/drivers/block/pktcdvd.c
@@ -117,8 +117,10 @@ static struct pktcdvd_kobj* pkt_kobj_cre
 	p->kobj.parent = parent;
 	p->kobj.ktype = ktype;
 	p->pd = pd;
-	if (kobject_register(&p->kobj) != 0)
+	if (kobject_register(&p->kobj) != 0) {
+		kobject_put(&p->kobj);
 		return NULL;
+	}
 	return p;
 }
 /*