stresstest.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (C) 2006-2008 Nokia Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; see the file COPYING. If not, write to the Free Software
  15. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Test random reads, writes and erases on MTD device.
  18. *
  19. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/err.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/random.h>
  31. #include "mtd_test.h"
  32. static int dev = -EINVAL;
  33. module_param(dev, int, S_IRUGO);
  34. MODULE_PARM_DESC(dev, "MTD device number to use");
  35. static int count = 10000;
  36. module_param(count, int, S_IRUGO);
  37. MODULE_PARM_DESC(count, "Number of operations to do (default is 10000)");
  38. static struct mtd_info *mtd;
  39. static unsigned char *writebuf;
  40. static unsigned char *readbuf;
  41. static unsigned char *bbt;
  42. static int *offsets;
  43. static int pgsize;
  44. static int bufsize;
  45. static int ebcnt;
  46. static int pgcnt;
  47. static int rand_eb(void)
  48. {
  49. unsigned int eb;
  50. again:
  51. eb = prandom_u32();
  52. /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
  53. eb %= (ebcnt - 1);
  54. if (bbt[eb])
  55. goto again;
  56. return eb;
  57. }
  58. static int rand_offs(void)
  59. {
  60. unsigned int offs;
  61. offs = prandom_u32();
  62. offs %= bufsize;
  63. return offs;
  64. }
  65. static int rand_len(int offs)
  66. {
  67. unsigned int len;
  68. len = prandom_u32();
  69. len %= (bufsize - offs);
  70. return len;
  71. }
  72. static int do_read(void)
  73. {
  74. int eb = rand_eb();
  75. int offs = rand_offs();
  76. int len = rand_len(offs);
  77. loff_t addr;
  78. if (bbt[eb + 1]) {
  79. if (offs >= mtd->erasesize)
  80. offs -= mtd->erasesize;
  81. if (offs + len > mtd->erasesize)
  82. len = mtd->erasesize - offs;
  83. }
  84. addr = (loff_t)eb * mtd->erasesize + offs;
  85. return mtdtest_read(mtd, addr, len, readbuf);
  86. }
  87. static int do_write(void)
  88. {
  89. int eb = rand_eb(), offs, err, len;
  90. loff_t addr;
  91. offs = offsets[eb];
  92. if (offs >= mtd->erasesize) {
  93. err = mtdtest_erase_eraseblock(mtd, eb);
  94. if (err)
  95. return err;
  96. offs = offsets[eb] = 0;
  97. }
  98. len = rand_len(offs);
  99. len = ((len + pgsize - 1) / pgsize) * pgsize;
  100. if (offs + len > mtd->erasesize) {
  101. if (bbt[eb + 1])
  102. len = mtd->erasesize - offs;
  103. else {
  104. err = mtdtest_erase_eraseblock(mtd, eb + 1);
  105. if (err)
  106. return err;
  107. offsets[eb + 1] = 0;
  108. }
  109. }
  110. addr = (loff_t)eb * mtd->erasesize + offs;
  111. err = mtdtest_write(mtd, addr, len, writebuf);
  112. if (unlikely(err))
  113. return err;
  114. offs += len;
  115. while (offs > mtd->erasesize) {
  116. offsets[eb++] = mtd->erasesize;
  117. offs -= mtd->erasesize;
  118. }
  119. offsets[eb] = offs;
  120. return 0;
  121. }
  122. static int do_operation(void)
  123. {
  124. if (prandom_u32() & 1)
  125. return do_read();
  126. else
  127. return do_write();
  128. }
  129. static int __init mtd_stresstest_init(void)
  130. {
  131. int err;
  132. int i, op;
  133. uint64_t tmp;
  134. printk(KERN_INFO "\n");
  135. printk(KERN_INFO "=================================================\n");
  136. if (dev < 0) {
  137. pr_info("Please specify a valid mtd-device via module parameter\n");
  138. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  139. return -EINVAL;
  140. }
  141. pr_info("MTD device: %d\n", dev);
  142. mtd = get_mtd_device(NULL, dev);
  143. if (IS_ERR(mtd)) {
  144. err = PTR_ERR(mtd);
  145. pr_err("error: cannot get MTD device\n");
  146. return err;
  147. }
  148. if (mtd->writesize == 1) {
  149. pr_info("not NAND flash, assume page size is 512 "
  150. "bytes.\n");
  151. pgsize = 512;
  152. } else
  153. pgsize = mtd->writesize;
  154. tmp = mtd->size;
  155. do_div(tmp, mtd->erasesize);
  156. ebcnt = tmp;
  157. pgcnt = mtd->erasesize / pgsize;
  158. pr_info("MTD device size %llu, eraseblock size %u, "
  159. "page size %u, count of eraseblocks %u, pages per "
  160. "eraseblock %u, OOB size %u\n",
  161. (unsigned long long)mtd->size, mtd->erasesize,
  162. pgsize, ebcnt, pgcnt, mtd->oobsize);
  163. if (ebcnt < 2) {
  164. pr_err("error: need at least 2 eraseblocks\n");
  165. err = -ENOSPC;
  166. goto out_put_mtd;
  167. }
  168. /* Read or write up 2 eraseblocks at a time */
  169. bufsize = mtd->erasesize * 2;
  170. err = -ENOMEM;
  171. readbuf = vmalloc(bufsize);
  172. writebuf = vmalloc(bufsize);
  173. offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL);
  174. if (!readbuf || !writebuf || !offsets)
  175. goto out;
  176. for (i = 0; i < ebcnt; i++)
  177. offsets[i] = mtd->erasesize;
  178. prandom_bytes(writebuf, bufsize);
  179. bbt = kzalloc(ebcnt, GFP_KERNEL);
  180. if (!bbt)
  181. goto out;
  182. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  183. if (err)
  184. goto out;
  185. /* Do operations */
  186. pr_info("doing operations\n");
  187. for (op = 0; op < count; op++) {
  188. if ((op & 1023) == 0)
  189. pr_info("%d operations done\n", op);
  190. err = do_operation();
  191. if (err)
  192. goto out;
  193. err = mtdtest_relax();
  194. if (err)
  195. goto out;
  196. }
  197. pr_info("finished, %d operations done\n", op);
  198. out:
  199. kfree(offsets);
  200. kfree(bbt);
  201. vfree(writebuf);
  202. vfree(readbuf);
  203. out_put_mtd:
  204. put_mtd_device(mtd);
  205. if (err)
  206. pr_info("error %d occurred\n", err);
  207. printk(KERN_INFO "=================================================\n");
  208. return err;
  209. }
  210. module_init(mtd_stresstest_init);
  211. static void __exit mtd_stresstest_exit(void)
  212. {
  213. return;
  214. }
  215. module_exit(mtd_stresstest_exit);
  216. MODULE_DESCRIPTION("Stress test module");
  217. MODULE_AUTHOR("Adrian Hunter");
  218. MODULE_LICENSE("GPL");