From: Andrew Morton <akpm@linux-foundation.org>
To: WANG Cong <xiyou.wangcong@gmail.com>
Cc: Cornelia Huck <cornelia.huck@de.ibm.com>,
linux-kernel@vger.kernel.org, viro@zeniv.linux.org.uk
Subject: Re: [-mm3 PATCH] (Retry) Check the return value of kobject_add and etc.
Date: Tue, 10 Apr 2007 15:18:15 -0700 [thread overview]
Message-ID: <20070410151815.906ef9b4.akpm@linux-foundation.org> (raw)
In-Reply-To: <20070410140829.GA3382@localhost.localdomain>
On Tue, 10 Apr 2007 22:08:29 +0800
WANG Cong <xiyou.wangcong@gmail.com> wrote:
> Since kobject_add, sysfs_create_link and sysfs_create_file are marked as '__must_check', we must always check their return values.
>
> Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>
> Acked-by: Cornelia Huck <cornelia.huck@de.ibm.com>
>
> ---
> --- linux-2.6.21-rc5-mm4/fs/partitions/check.c.orig 2007-04-05 12:48:29.000000000 +0800
> +++ linux-2.6.21-rc5-mm4/fs/partitions/check.c 2007-04-05 23:15:41.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)) {
> + kobject_put(&p->kobj);
> + 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);
> + kobject_put(&p->kobj);
> + return;
> + }
> if (flags & ADDPART_FLAG_WHOLEDISK) {
> static struct attribute addpartattr = {
> .name = "whole_disk",
> @@ -396,7 +404,13 @@ void add_partition(struct gendisk *disk,
> .owner = THIS_MODULE,
> };
>
> - sysfs_create_file(&p->kobj, &addpartattr);
> + if (sysfs_create_file(&p->kobj, &addpartattr)) {
> + sysfs_remove_link(&p->kobj, "subsystem");
> + kobject_uevent(&p->kobj, KOBJ_REMOVE);
> + kobject_del(&p->kobj);
> + kobject_put(&p->kobj);
> + return;
> + }
> }
> partition_sysfs_add_subdir(p);
> disk->part[part-1] = p;
Your mail client replaces tabs with spaces - please fix it.
The code duplication is unpleasing. We normally do this as below (please
review this code).
Note that I changed it to not send the KOBJ_REMOVE if we didn't send a
KOBJ_ADD.
From: WANG Cong <xiyou.wangcong@gmail.com>
Since kobject_add, sysfs_create_link and sysfs_create_file are marked as
'__must_check', we must always check their return values.
Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>
Acked-by: Cornelia Huck <cornelia.huck@de.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---
fs/partitions/check.c | 25 ++++++++++++++++++++-----
1 files changed, 20 insertions(+), 5 deletions(-)
diff -puN fs/partitions/check.c~partitions-check-the-return-value-of-kobject_add-etc fs/partitions/check.c
--- a/fs/partitions/check.c~partitions-check-the-return-value-of-kobject_add-etc
+++ a/fs/partitions/check.c
@@ -383,26 +383,41 @@ void add_partition(struct gendisk *disk,
p->policy = disk->policy;
if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1]))
- snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part);
+ snprintf(p->kobj.name, KOBJ_NAME_LEN, "%sp%d",
+ disk->kobj.name, part);
else
- snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part);
+ snprintf(p->kobj.name, KOBJ_NAME_LEN, "%s%d",
+ disk->kobj.name, part);
p->kobj.parent = &disk->kobj;
p->kobj.ktype = &ktype_part;
kobject_init(&p->kobj);
- kobject_add(&p->kobj);
+ if (kobject_add(&p->kobj))
+ goto out_put;
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"))
+ goto out_uevent;
if (flags & ADDPART_FLAG_WHOLEDISK) {
static struct attribute addpartattr = {
.name = "whole_disk",
.mode = S_IRUSR | S_IRGRP | S_IROTH,
};
- sysfs_create_file(&p->kobj, &addpartattr);
+ if (sysfs_create_file(&p->kobj, &addpartattr))
+ goto out_link;
}
partition_sysfs_add_subdir(p);
disk->part[part-1] = p;
+ return;
+
+out_link:
+ sysfs_remove_link(&p->kobj, "subsystem");
+out_uevent:
+ if (!disk->part_uevent_suppress)
+ kobject_uevent(&p->kobj, KOBJ_REMOVE);
+ kobject_del(&p->kobj);
+out_put:
+ kobject_put(&p->kobj);
}
static char *make_block_name(struct gendisk *disk)
_
next prev parent reply other threads:[~2007-04-10 22:18 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-01 7:32 [-mm3 PATCH] (Retry) Check the return value of kobject_add and etc Cong WANG
2007-04-02 11:01 ` Cornelia Huck
2007-04-05 4:54 ` WANG Cong
2007-04-05 9:11 ` Cornelia Huck
2007-04-05 14:44 ` WANG Cong
2007-04-05 15:05 ` Cornelia Huck
2007-04-05 15:27 ` WANG Cong
2007-04-05 16:00 ` Cornelia Huck
2007-04-06 2:53 ` WANG Cong
2007-04-10 12:31 ` Cornelia Huck
2007-04-10 12:35 ` [Patch -mm] kobject: kobject_add() reference leak Cornelia Huck
2007-04-10 14:08 ` [-mm3 PATCH] (Retry) Check the return value of kobject_add and etc WANG Cong
2007-04-10 22:18 ` Andrew Morton [this message]
2007-04-11 6:45 ` WANG Cong
2007-04-11 7:19 ` Andrew Morton
2007-04-11 9:24 ` Cornelia Huck
-- strict thread matches above, loose matches on Subject: below --
2007-04-01 8:22 Parag Warudkar
2007-04-01 13:46 ` Parag Warudkar
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=20070410151815.906ef9b4.akpm@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=cornelia.huck@de.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=xiyou.wangcong@gmail.com \
/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