Commit b802c074 authored by Thago Galesi's avatar Thago Galesi Committed by David Woodhouse

[PATCH] Remove unnecessary kmalloc/kfree calls in mtdchar

This patch removes repeated calls to kmalloc / kfree in mtd_write /
mtd_read functions, replacing them by a single kmalloc / kfree pair.
Signed-off-by: default avatarThiago Galesi <thiagogalesi@gmail.com>
Signed-off-by: default avatarDavid Woodhouse <dwmw2@infradead.org>
parent cd2866fa
...@@ -170,16 +170,22 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t ...@@ -170,16 +170,22 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t
/* FIXME: Use kiovec in 2.5 to lock down the user's buffers /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
and pass them directly to the MTD functions */ and pass them directly to the MTD functions */
while (count) {
if (count > MAX_KMALLOC_SIZE) if (count > MAX_KMALLOC_SIZE)
len = MAX_KMALLOC_SIZE; kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
else else
len = count; kbuf=kmalloc(count, GFP_KERNEL);
kbuf=kmalloc(len,GFP_KERNEL);
if (!kbuf) if (!kbuf)
return -ENOMEM; return -ENOMEM;
while (count) {
if (count > MAX_KMALLOC_SIZE)
len = MAX_KMALLOC_SIZE;
else
len = count;
switch (MTD_MODE(file)) { switch (MTD_MODE(file)) {
case MTD_MODE_OTP_FACT: case MTD_MODE_OTP_FACT:
ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf); ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
...@@ -215,9 +221,9 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t ...@@ -215,9 +221,9 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t
return ret; return ret;
} }
kfree(kbuf);
} }
kfree(kbuf);
return total_retlen; return total_retlen;
} /* mtd_read */ } /* mtd_read */
...@@ -241,18 +247,21 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count ...@@ -241,18 +247,21 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count
if (!count) if (!count)
return 0; return 0;
if (count > MAX_KMALLOC_SIZE)
kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
else
kbuf=kmalloc(count, GFP_KERNEL);
if (!kbuf)
return -ENOMEM;
while (count) { while (count) {
if (count > MAX_KMALLOC_SIZE) if (count > MAX_KMALLOC_SIZE)
len = MAX_KMALLOC_SIZE; len = MAX_KMALLOC_SIZE;
else else
len = count; len = count;
kbuf=kmalloc(len,GFP_KERNEL);
if (!kbuf) {
printk("kmalloc is null\n");
return -ENOMEM;
}
if (copy_from_user(kbuf, buf, len)) { if (copy_from_user(kbuf, buf, len)) {
kfree(kbuf); kfree(kbuf);
return -EFAULT; return -EFAULT;
...@@ -282,10 +291,9 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count ...@@ -282,10 +291,9 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count
kfree(kbuf); kfree(kbuf);
return ret; return ret;
} }
kfree(kbuf);
} }
kfree(kbuf);
return total_retlen; return total_retlen;
} /* mtd_write */ } /* mtd_write */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment