Retry Large Buffer Allocations
PATCH v4 1/1 Retry Large Buffer Allocations
When handling user space read or write requests via mtd_{read,write} or JFFS2 medium scan requests, exponentially back off on the size of the requested kernel transfer buffer until it succeeds or until the requested transfer buffer size falls below the page size.
This helps ensure the operation can succeed under low-memory, highly-fragmented situations albeit somewhat more slowly.
v2: Incorporated coding style and comment feedback from Artem.
v3: Incorporated more feedback from Artem. Retargeted patch against
l2-mtd-2.6.
v4: Incorporated feedback from Artem about compilation on 64-bit
architectures.
--- drivers/mtd/mtdchar.c | 50 +++++++++++++++++++++------------------------- drivers/mtd/mtdcore.c | 41 ++++++++++++++++++++++++++++++++++++++ fs/jffs2/scan.c | 19 ++++++++++------- include/linux/mtd/mtd.h | 2 + 4 files changed, 77 insertions(+), 35 deletions(-)
drivers/mtd/mtdchar.c +23 -27
@@ -166,10 +166,23 @@ static int mtd_close(struct inode *inode, struct file *file)return 0 ; }/* mtd_close */ -/* FIXME: This _really_ needs to die. In 2.5, we should lock the -userspace buffer down and use it directly with readv/writev. -*/ -#define MAX_KMALLOC_SIZE 0x20000 +/* Back in April 2005, Linus wrote: +* +* FIXME: This _really_ needs to die. In 2.5, we should lock the +* userspace buffer down and use it directly with readv/writev. +* +* The implementation below, using mtd_kmalloc_up_to, mitigates +* allocation failures when the system is under low-memory situations +* or if memory is highly fragmented at the cost of reducing the +* performance of the requested transfer due to a smaller buffer size. +* +* A more complex but more memory-efficient implementation based on +* get_user_pages and iovecs to cover extents of those pages is a +* longer-term goal, as intimated by Linus above. However, for the +* write case, this requires yet more complex head and tail transfer +* handling when those head and tail offsets and sizes are such that +* alignment requirements are not met in the NAND subdriver. +*/ static ssize_t mtd_read (struct file *file,char __user *buf,size_t count,loff_t *ppos) {
@@ -179,6 +192,7 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_tsize_t total_retlen=0 ;int ret=0 ;int len; +size_t size = count;char *kbuf;DEBUG (MTD_DEBUG_LEVEL0 ,"MTD_read\n" );
@@ -189,23 +203,12 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_tif (!count)return 0 ; -/* FIXME: Use kiovec in 2.5 to lock down the user's buffers -and pass them directly to the MTD functions */ - -if (count >MAX_KMALLOC_SIZE ) - kbuf=kmalloc (MAX_KMALLOC_SIZE ,GFP_KERNEL ); -else - kbuf=kmalloc (count,GFP_KERNEL ); - + kbuf =mtd_kmalloc_up_to (&size);if (!kbuf)return -ENOMEM ;while (count) { - -if (count >MAX_KMALLOC_SIZE ) - len =MAX_KMALLOC_SIZE ; -else - len = count; + len =min_t (size_t, count, size);switch (mfi->mode) {case MTD_MODE_OTP_FACTORY :
@@ -268,6 +271,7 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count {struct mtd_file_info *mfi = file->private_data;struct mtd_info *mtd = mfi->mtd; +size_t size = count;char *kbuf;size_t retlen;size_t total_retlen=0 ;
@@ -285,20 +289,12 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t countif (!count)return 0 ; -if (count >MAX_KMALLOC_SIZE ) - kbuf=kmalloc (MAX_KMALLOC_SIZE ,GFP_KERNEL ); -else - kbuf=kmalloc (count,GFP_KERNEL ); - + kbuf =mtd_kmalloc_up_to (&size);if (!kbuf)return -ENOMEM ;while (count) { - -if (count >MAX_KMALLOC_SIZE ) - len =MAX_KMALLOC_SIZE ; -else - len = count; + len =min_t (size_t, count, size);if (copy_from_user (kbuf, buf, len)) {kfree (kbuf);
drivers/mtd/mtdcore.c +41 -0
53 lines not on this page · raw message
fs/jffs2/scan.c +11 -8
@@ -94,7 +94,7 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)uint32_t buf_size =0 ;struct jffs2_summary *s =NULL ;/* summary info collected by the scan process */ #ifndef __ECOS -size_t pointlen; +size_t pointlen, try_size;if (c->mtd->point) { ret = c->mtd->point(c->mtd, 0, c->mtd->size, &pointlen,
@@ -113,18 +113,21 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)/* For NAND it's quicker to read a whole eraseblock at a time, apparently */ if (jffs2_cleanmarker_oob (c)) - buf_size = c->sector_size; + try_size = c->sector_size;else - buf_size =PAGE_SIZE ; + try_size =PAGE_SIZE ; -/* Respect kmalloc limitations */ -if (buf_size >128 *1024 ) - buf_size =128 *1024 ; +D1 (printk (KERN_DEBUG "Trying to allocate readbuf of %zu " +"bytes\n" , try_size)); -D1 (printk (KERN_DEBUG "Allocating readbuf of %d bytes\n" , buf_size)); - flashbuf =kmalloc (buf_size,GFP_KERNEL ); + flashbuf =mtd_kmalloc_up_to (&try_size);if (!flashbuf)return -ENOMEM ; + +D1 (printk (KERN_DEBUG "Allocated readbuf of %zu bytes\n" , + try_size)); + + buf_size = (uint32_t )try_size; }if (jffs2_sum_active ()) {
include/linux/mtd/mtd.h +2 -1
@@ -348,7 +348,8 @@ int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,int default_mtd_readv (struct mtd_info *mtd,struct kvec *vecs,unsigned long count,loff_t from,size_t *retlen); +void *mtd_kmalloc_up_to (size_t *size); +#ifdef CONFIG_MTD_PARTITIONS void mtd_erase_callback (struct erase_info *instr);#else --
1.7.4.2
Quoted 14 lines · Grant Erickson
When handling user space read or write requests via mtd_{read,write} or JFFS2 medium scan requests, exponentially back off on the size of the requested kernel transfer buffer until it succeeds or until the requested transfer buffer size falls below the page size.
This helps ensure the operation can succeed under low-memory, highly-fragmented situations albeit somewhat more slowly.
v2: Incorporated coding style and comment feedback from Artem.
v3: Incorporated more feedback from Artem. Retargeted patch againstl2-mtd-2.6.v4: Incorporated feedback from Artem about compilation on 64-bit
architectures.
Quoted 7 lines
Signed-off-by Grant Erickson <[email protected]>--- drivers/mtd/mtdchar.c | 50 +++++++++++++++++++++------------------------- drivers/mtd/mtdcore.c | 41 ++++++++++++++++++++++++++++++++++++++ fs/jffs2/scan.c | 19 ++++++++++------- include/linux/mtd/mtd.h | 2 + 4 files changed, 77 insertions(+), 35 deletions(-)
Ideally, you should have split this on 3 patches and send a series, but well, I already kind of said that were the last requests, so I do not push for this. I've just taken this to my l2-mtd-2.6.git.
But if you can, please split the patch on 3 patches:
1. Introduces mtd_kmalloc_up_to - explain the reasons we need this func.
2. Start using it in mtdcore
3. Start using it in JFFS2
This is nicer way.
But if you do not do this, I do not mind. Thanks!
Signature · 2 lines
Best Regards, Artem Bityutskiy (Артём Битюцкий)
Hi Grant, Artem
Thanks for this fix!
Quoted 8 lines · Grant Erickson
When handling user space read or write requests via mtd_{read,write} or JFFS2 medium scan requests, exponentially back off on the size of the requested kernel transfer buffer until it succeeds or until the requested transfer buffer size falls below the page size.
This helps ensure the operation can succeed under low-memory, highly-fragmented situations albeit somewhat more slowly.
Quoted 7 lines
v2: Incorporated coding style and comment feedback from Artem.
v3: Incorporated more feedback from Artem. Retargeted patch against l2-mtd-2.6.
v4: Incorporated feedback from Artem about compilation on 64-bit architectures.Signed-off-by Grant Erickson <[email protected]>
I tested this on da850evm by cherry-picking commit 64542ac252d378dc7a1122064821a884e85a9954 fromgit://git.infradead.org/users/dedekind/l2-mtd-2.6.git.
I have a memory hole @32M (bootargs: mem=32M@0xc0000000 mem=64M@0xc4000000) and I was also (like Bastian) getting "ubiformat:
page allocation failure" errors. Cherry-picking this commit fixed the problem for me; I was able to do 3x repeated ubiformats without error.
Best Regards,
Ben Gardiner
---
Nanometrics Inc.
http://www.nanometrics.ca
Quoted 10 lines · Ben Gardiner
I tested this on da850evm by cherry-picking commit
64542ac252d378dc7a1122064821a884e85a9954fromgit://git.infradead.org/users/dedekind/l2-mtd-2.6.git.I have a memory hole @32M (bootargs: mem=32M@0xc0000000 mem=64M@0xc4000000) and I was also (like Bastian) getting "ubiformat:
page allocation failure" errors. Cherry-picking this commit fixed the problem for me; I was able to do 3x repeated ubiformats without error.Tested-by Ben Gardiner <[email protected]>
Great, thanks!
Signature · 2 lines
Best Regards, Artem Bityutskiy (Артём Битюцкий)
Quoted 19 lines · Ben Gardiner
Hi Grant, Artem
Thanks for this fix!
Quoted 14 lines · Grant Erickson
When handling user space read or write requests via mtd_{read,write} or JFFS2 medium scan requests, exponentially back off on the size of the requested kernel transfer buffer until it succeeds or until the requested transfer buffer size falls below the page size.
This helps ensure the operation can succeed under low-memory, highly-fragmented situations albeit somewhat more slowly.
v2: Incorporated coding style and comment feedback from Artem.
v3: Incorporated more feedback from Artem. Retargeted patch againstl2-mtd-2.6.v4: Incorporated feedback from Artem about compilation on 64-bit
architectures.
Quoted 5 lines
Quoted 1 line
Signed-off-by Grant Erickson <[email protected]>I tested this on da850evm by cherry-picking commit
64542ac252d378dc7a1122064821a884e85a9954fromgit://git.infradead.org/users/dedekind/l2-mtd-2.6.git.
Grant sent an updated patch-set and added your Tested-by. However, there were several conflicts which I had to. If you could test the final patches again - that'd be great! They are in the same l2-mtd-2.6.git tree:
8fa3c20 JFFS2: retry large buffer allocations 88ab142 mtd: mtdchar: retry large buffer allocations 99815a5 mtd: create function to perform large allocations
Signature · 2 lines
Best Regards, Artem Bityutskiy (Артём Битюцкий)