public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: wzt.wzt@gmail.com
To: linux-kernel@vger.kernel.org
Cc: axboe@kernel.dk
Subject: [PATCH] Block: Check major number before allocate the buffer in register_blkdev()
Date: Mon, 10 May 2010 14:16:02 +0800	[thread overview]
Message-ID: <20100510061602.GB2858@localhost.localdomain> (raw)

Check major number before allocate the buffer, if the major number is not exist,
and the register_blkdev() called many times, kmalloc()/kfree() will be no need 
to invoked many times. So check the major number before use kmalloc() to allocate
the buffer will be better.

Signed-off-by: Zhitong Wang <zhitong.wangzt@alibaba-inc.com>

---
 block/genhd.c |   25 ++++++++++++-------------
 1 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/block/genhd.c b/block/genhd.c
index d13ba76..15360cc 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -300,30 +300,29 @@ int register_blkdev(unsigned int major, const char *name)
 		ret = major;
 	}
 
-	p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
-	if (p == NULL) {
-		ret = -ENOMEM;
-		goto out;
-	}
-
-	p->major = major;
-	strlcpy(p->name, name, sizeof(p->name));
-	p->next = NULL;
 	index = major_to_index(major);
 
 	for (n = &major_names[index]; *n; n = &(*n)->next) {
 		if ((*n)->major == major)
 			break;
 	}
-	if (!*n)
+	if (!*n) {
+		p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
+		if (p == NULL) {
+			ret = -ENOMEM;
+			goto out;
+		}
+
+		p->major = major;
+		strlcpy(p->name, name, sizeof(p->name));
+		p->next = NULL;
 		*n = p;
-	else
+	}
+	else {
 		ret = -EBUSY;
 
-	if (ret < 0) {
 		printk("register_blkdev: cannot get major %d for %s\n",
 		       major, name);
-		kfree(p);
 	}
 out:
 	mutex_unlock(&block_class_lock);
-- 
1.6.5.3


             reply	other threads:[~2010-05-10  6:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-10  6:16 wzt.wzt [this message]
2010-05-10 11:56 ` [PATCH] Block: Check major number before allocate the buffer in register_blkdev() Jens Axboe
2010-05-10 12:24   ` wzt wzt
2010-05-10 12:35     ` Jens Axboe

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=20100510061602.GB2858@localhost.localdomain \
    --to=wzt.wzt@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=linux-kernel@vger.kernel.org \
    /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