Longs spinlock on NOR reads
0123456789012345678901234567890123456789012345678901234567890123456789012345 6789
Dear MTD,
I am working with a customer with an embedded ARM-9 processor in a real time
system. The system is running a stock 2.6.18 kernel and uses a NOR device for
its main storage.
We have noticed that when we perform large reads (> 32KB) from the FLASH using
the /dev/mtdN device, the real-time performance of the system is impacted and
interrupts get held off. We did some digging and found that the kernel will
permit reads up to 128KB in size to be made via the mtd driver in a single read
call. This in turn passes through the mtd driver and ends up at the chip code.
In our case, we are using the 'cfi_cmdset_0002.c' driver. When the call arrives
at cfi_amstsd_read(), since it is within the single NOR chip that we are using,
a single call is made to do_read_onechip(). This function takes a spinlock,
performs the FULL memory copy and releases the spinlock. It is the long duration of the spinlock over the full memory copy that is impacting our real-time performance.
If a NOR chip has ~70ns access time then an efficient memory subsystem should
be able to make ~10M accesses per second. If the chip is 16-bits wide then a 128KB copy will ~7ms. This is a long time to hold a spinlock in a real-time system. We inspected the mtd driver code and it will only fragment
transfers over 128KB in length.
This problem does not occur with accesses to /dev/mtdblockN devices because they are automatically broken into 512 byte sectors. It is a problem with /dev/mtdN devices and also with JFFS2 operations.
We have a simple fix that splits up read accesses into 512 byte chunks withincfi_amdstd_read() and performs multiple calls to do_read_onechip() until the full data length has been copied. There is no change to the external API and
the overall performance does not seem to be noticeably degraded even though a lot more calls to do_read_onechip() are required.
Before going through the process of submitting patches etc., is this a problem that has been identified or experienced before? I have searched the mailing list but not seen any obvious references...
If it seems a reasonable observation and fix, would you like me to post a patch? I can only patch and test the 'cfi_cmdset_0002.c' driver but it would be a simple process for it to be applied to the other chips.
Best regards,
Roger
Quoted 4 lines · Roger Lucas
Before going through the process of submitting patches etc., is this a problem that has been identified or experienced before? I have searched the mailing list but not seen any obvious references...
I haven't seen the problem you are describing, but I haven't studied the performance of JFFS2 down at those time levels. Your problem description sounds very reasonable, however, as does the proposed fix.
I think you'll probably get down to 7ms only if the compiler is doing a lot of ldm/stm, which I'd be surprised to see. But the overhead of a function call for every 512 bytes might be measurable--- is there a point within the lowest-level code that might be a good place to briefly unlock and then re-lock the spinlock? That would reduce the overhead of the fix by avoiding the function call.
Regardless, if someone is paying attention to the numbers then they're probably more concerned with the latency than throughput. In that case, if the only way to substantially improve latency is to take a per-512-byte function call hit then I (for one) can live with that.
Quoted 4 lines
If it seems a reasonable observation and fix, would you like me to post a patch? I can only patch and test the '
cfi_cmdset_0002.c' driver but it would be a simple process for it to be applied to the other chips.
Definitely post the patch. Can't reject something we haven't seen. :)
Maybe it needs to be surrounded by a CONFIG_MTD_<something> so that people can turn it on or off via menuconfig.
b.g.
Hi Bill,
Firstly, many apologies that it has taken me so long to provide the patch... It is very small but I have had so many other things on, I just didn't get around to it.
The patch only applies to the one FLASH chip that we have one the system we are using. I'm sure something similar may be appropriate for the other chips but I don't want to offer a patch against something that I haven't tried and cannot test.
Best regards,
Roger
-----Original Message-----
From: Bill Gatliff [mailto:[email protected]]
Sent: 26 June 2009 00:21
To: Roger Lucas
Cc: [email protected]
Subject: Re: Longs spinlock on NOR reads
Quoted 2 lines · Roger Lucas
Before going through the process of submitting patches etc., is this a problem that has been identified or experienced before? I have searched
the
Quoted 2 lines
mailing list but not seen any obvious references...
I haven't seen the problem you are describing, but I haven't studied the performance of JFFS2 down at those time levels. Your problem description sounds very reasonable, however, as does the proposed fix.
I think you'll probably get down to 7ms only if the compiler is doing a lot of ldm/stm, which I'd be surprised to see. But the overhead of a function call for every 512 bytes might be measurable--- is there a point within the lowest-level code that might be a good place to briefly unlock and then re-lock the spinlock? That would reduce the overhead of the fix by avoiding the function call.
Regardless, if someone is paying attention to the numbers then they're probably more concerned with the latency than throughput. In that case, if the only way to substantially improve latency is to take a per-512-byte function call hit then I (for one) can live with that.
Quoted 4 lines
If it seems a reasonable observation and fix, would you like me to post a patch? I can only patch and test the '
cfi_cmdset_0002.c' driver but it would be a simple process for it to be applied to the other chips.
Definitely post the patch. Can't reject something we haven't seen. :)
Maybe it needs to be surrounded by a CONFIG_MTD_<something> so that people can turn it on or off via menuconfig.
b.g.
Checked by AVG - www.avg.com
Version: 8.5.375 / Virus Database: 270.12.92/2202 - Release Date: 06/25/09 17:58:00
drivers/mtd/chips/cfi_cmdset_0002.c 2009-07-28 12:01:09.000000000 +0100 renamed from drivers/mtd/chips/cfi_cmdset_0002.c 2009-06-10 04:05:27.000000000 +0100 +13 -6
@@ -926,6 +926,7 @@ return 0; } +#define MAX_READ_BURST 1024 static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) {
@@ -954,13 +955,19 @@ else thislen = len; - ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf); - if (ret) - break; + while(thislen) { + unsigned long burstlen = (thislen < MAX_READ_BURST)?thislen:MAX_READ_BURST; - *retlen += thislen; - len -= thislen; - buf += thislen; + ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, burstlen, buf); + if (ret) + break; + + *retlen += burstlen; + len -= burstlen; + buf += burstlen; + ofs += burstlen; + thislen -= burstlen; + } ofs = 0; chipnum++;