stresstest.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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), err;
  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 = eb * mtd->erasesize + offs;
  85. err = mtdtest_read(mtd, addr, len, readbuf);
  86. if (unlikely(err)) {
  87. pr_err("error: read failed at 0x%llx\n",
  88. (long long)addr);
  89. return err;
  90. }
  91. return 0;
  92. }
  93. static int do_write(void)
  94. {
  95. int eb = rand_eb(), offs, err, len;
  96. loff_t addr;
  97. offs = offsets[eb];
  98. if (offs >= mtd->erasesize) {
  99. err = mtdtest_erase_eraseblock(mtd, eb);
  100. if (err)
  101. return err;
  102. offs = offsets[eb] = 0;
  103. }
  104. len = rand_len(offs);
  105. len = ((len + pgsize - 1) / pgsize) * pgsize;
  106. if (offs + len > mtd->erasesize) {
  107. if (bbt[eb + 1])
  108. len = mtd->erasesize - offs;
  109. else {
  110. err = mtdtest_erase_eraseblock(mtd, eb + 1);
  111. if (err)
  112. return err;
  113. offsets[eb + 1] = 0;
  114. }
  115. }
  116. addr = eb * mtd->erasesize + offs;
  117. err = mtdtest_write(mtd, addr, len, writebuf);
  118. if (unlikely(err)) {
  119. pr_err("error: write failed at 0x%llx\n",
  120. (long long)addr);
  121. return err;
  122. }
  123. offs += len;
  124. while (offs > mtd->erasesize) {
  125. offsets[eb++] = mtd->erasesize;
  126. offs -= mtd->erasesize;
  127. }
  128. offsets[eb] = offs;
  129. return 0;
  130. }
  131. static int do_operation(void)
  132. {
  133. if (prandom_u32() & 1)
  134. return do_read();
  135. else
  136. return do_write();
  137. }
  138. static int __init mtd_stresstest_init(void)
  139. {
  140. int err;
  141. int i, op;
  142. uint64_t tmp;
  143. printk(KERN_INFO "\n");
  144. printk(KERN_INFO "=================================================\n");
  145. if (dev < 0) {
  146. pr_info("Please specify a valid mtd-device via module parameter\n");
  147. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  148. return -EINVAL;
  149. }
  150. pr_info("MTD device: %d\n", dev);
  151. mtd = get_mtd_device(NULL, dev);
  152. if (IS_ERR(mtd)) {
  153. err = PTR_ERR(mtd);
  154. pr_err("error: cannot get MTD device\n");
  155. return err;
  156. }
  157. if (mtd->writesize == 1) {
  158. pr_info("not NAND flash, assume page size is 512 "
  159. "bytes.\n");
  160. pgsize = 512;
  161. } else
  162. pgsize = mtd->writesize;
  163. tmp = mtd->size;
  164. do_div(tmp, mtd->erasesize);
  165. ebcnt = tmp;
  166. pgcnt = mtd->erasesize / pgsize;
  167. pr_info("MTD device size %llu, eraseblock size %u, "
  168. "page size %u, count of eraseblocks %u, pages per "
  169. "eraseblock %u, OOB size %u\n",
  170. (unsigned long long)mtd->size, mtd->erasesize,
  171. pgsize, ebcnt, pgcnt, mtd->oobsize);
  172. if (ebcnt < 2) {
  173. pr_err("error: need at least 2 eraseblocks\n");
  174. err = -ENOSPC;
  175. goto out_put_mtd;
  176. }
  177. /* Read or write up 2 eraseblocks at a time */
  178. bufsize = mtd->erasesize * 2;
  179. err = -ENOMEM;
  180. readbuf = vmalloc(bufsize);
  181. writebuf = vmalloc(bufsize);
  182. offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL);
  183. if (!readbuf || !writebuf || !offsets)
  184. goto out;
  185. for (i = 0; i < ebcnt; i++)
  186. offsets[i] = mtd->erasesize;
  187. prandom_bytes(writebuf, bufsize);
  188. bbt = kzalloc(ebcnt, GFP_KERNEL);
  189. if (!bbt)
  190. goto out;
  191. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  192. if (err)
  193. goto out;
  194. /* Do operations */
  195. pr_info("doing operations\n");
  196. for (op = 0; op < count; op++) {
  197. if ((op & 1023) == 0)
  198. pr_info("%d operations done\n", op);
  199. err = do_operation();
  200. if (err)
  201. goto out;
  202. cond_resched();
  203. }
  204. pr_info("finished, %d operations done\n", op);
  205. out:
  206. kfree(offsets);
  207. kfree(bbt);
  208. vfree(writebuf);
  209. vfree(readbuf);
  210. out_put_mtd:
  211. put_mtd_device(mtd);
  212. if (err)
  213. pr_info("error %d occurred\n", err);
  214. printk(KERN_INFO "=================================================\n");
  215. return err;
  216. }
  217. module_init(mtd_stresstest_init);
  218. static void __exit mtd_stresstest_exit(void)
  219. {
  220. return;
  221. }
  222. module_exit(mtd_stresstest_exit);
  223. MODULE_DESCRIPTION("Stress test module");
  224. MODULE_AUTHOR("Adrian Hunter");
  225. MODULE_LICENSE("GPL");