lkml.org 
[lkml]   [2006]   [Apr]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
Subject[PATCH] Remove unnecessary kmalloc/kfree calls in mtdchar
This patch removes the use of repeated calls to kmalloc / kfree when
writing / reading from a MTD char device. Not the ideal solution
mentioned in the driver, but nonetheless better.

Signed-off by Thiago Galesi <thiagogalesi@gmail.com>

---

(Please CC me as I'm not subscribed to linux-mtd)

Index: linux-2.6.16.2/drivers/mtd/mtdchar.c
===================================================================
--- linux-2.6.16.2.orig/drivers/mtd/mtdchar.c
+++ linux-2.6.16.2/drivers/mtd/mtdchar.c
@@ -170,15 +170,18 @@ static ssize_t mtd_read(struct file *fil

/* FIXME: Use kiovec in 2.5 to lock down the user's buffers
and pass them directly to the MTD functions */
- while (count) {
- if (count > MAX_KMALLOC_SIZE)
- len = MAX_KMALLOC_SIZE;
- else
- len = count;

- kbuf=kmalloc(len,GFP_KERNEL);
- if (!kbuf)
- return -ENOMEM;
+ if (count > MAX_KMALLOC_SIZE)
+ len = MAX_KMALLOC_SIZE;
+ else
+ len = count;
+
+ kbuf=kmalloc(len,GFP_KERNEL);
+
+ if (!kbuf)
+ return -ENOMEM;
+
+ while (count) {

switch (MTD_MODE(file)) {
case MTD_MODE_OTP_FACT:
@@ -215,9 +218,9 @@ static ssize_t mtd_read(struct file *fil
return ret;
}

- kfree(kbuf);
}

+ kfree(kbuf);
return total_retlen;
} /* mtd_read */

@@ -241,17 +244,18 @@ static ssize_t mtd_write(struct file *fi
if (!count)
return 0;

- while (count) {
- if (count > MAX_KMALLOC_SIZE)
- len = MAX_KMALLOC_SIZE;
- else
- len = count;
+ if (count > MAX_KMALLOC_SIZE)
+ len = MAX_KMALLOC_SIZE;
+ else
+ len = count;
+
+ kbuf=kmalloc(len,GFP_KERNEL);
+ if (!kbuf) {
+ printk("kmalloc is null\n");
+ return -ENOMEM;
+ }

- kbuf=kmalloc(len,GFP_KERNEL);
- if (!kbuf) {
- printk("kmalloc is null\n");
- return -ENOMEM;
- }
+ while (count) {

if (copy_from_user(kbuf, buf, len)) {
kfree(kbuf);
@@ -282,10 +286,9 @@ static ssize_t mtd_write(struct file *fi
kfree(kbuf);
return ret;
}
-
- kfree(kbuf);
}

+ kfree(kbuf);
return total_retlen;
} /* mtd_write */
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
\
 
 \ /
  Last update: 2006-04-15 04:41    [W:0.070 / U:0.008 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site