1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006-2008 Nokia Corporation
5 * Test random reads, writes and erases on MTD device.
7 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/err.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/vmalloc.h>
20 #include <linux/random.h>
24 static int dev
= -EINVAL
;
25 module_param(dev
, int, S_IRUGO
);
26 MODULE_PARM_DESC(dev
, "MTD device number to use");
28 static int count
= 10000;
29 module_param(count
, int, S_IRUGO
);
30 MODULE_PARM_DESC(count
, "Number of operations to do (default is 10000)");
32 static struct mtd_info
*mtd
;
33 static unsigned char *writebuf
;
34 static unsigned char *readbuf
;
35 static unsigned char *bbt
;
43 static int rand_eb(void)
49 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
56 static int rand_offs(void)
65 static int rand_len(int offs
)
70 len
%= (bufsize
- offs
);
74 static int do_read(void)
77 int offs
= rand_offs();
78 int len
= rand_len(offs
);
82 if (offs
>= mtd
->erasesize
)
83 offs
-= mtd
->erasesize
;
84 if (offs
+ len
> mtd
->erasesize
)
85 len
= mtd
->erasesize
- offs
;
87 addr
= (loff_t
)eb
* mtd
->erasesize
+ offs
;
88 return mtdtest_read(mtd
, addr
, len
, readbuf
);
91 static int do_write(void)
93 int eb
= rand_eb(), offs
, err
, len
;
97 if (offs
>= mtd
->erasesize
) {
98 err
= mtdtest_erase_eraseblock(mtd
, eb
);
101 offs
= offsets
[eb
] = 0;
103 len
= rand_len(offs
);
104 len
= ((len
+ pgsize
- 1) / pgsize
) * pgsize
;
105 if (offs
+ len
> mtd
->erasesize
) {
107 len
= mtd
->erasesize
- offs
;
109 err
= mtdtest_erase_eraseblock(mtd
, eb
+ 1);
115 addr
= (loff_t
)eb
* mtd
->erasesize
+ offs
;
116 err
= mtdtest_write(mtd
, addr
, len
, writebuf
);
120 while (offs
> mtd
->erasesize
) {
121 offsets
[eb
++] = mtd
->erasesize
;
122 offs
-= mtd
->erasesize
;
128 static int do_operation(void)
130 if (prandom_u32() & 1)
136 static int __init
mtd_stresstest_init(void)
142 printk(KERN_INFO
"\n");
143 printk(KERN_INFO
"=================================================\n");
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");
151 pr_info("MTD device: %d\n", dev
);
153 mtd
= get_mtd_device(NULL
, dev
);
156 pr_err("error: cannot get MTD device\n");
160 if (mtd
->writesize
== 1) {
161 pr_info("not NAND flash, assume page size is 512 "
165 pgsize
= mtd
->writesize
;
168 do_div(tmp
, mtd
->erasesize
);
170 pgcnt
= mtd
->erasesize
/ pgsize
;
172 pr_info("MTD device size %llu, eraseblock size %u, "
173 "page size %u, count of eraseblocks %u, pages per "
174 "eraseblock %u, OOB size %u\n",
175 (unsigned long long)mtd
->size
, mtd
->erasesize
,
176 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
179 pr_err("error: need at least 2 eraseblocks\n");
184 /* Read or write up 2 eraseblocks at a time */
185 bufsize
= mtd
->erasesize
* 2;
188 readbuf
= vmalloc(bufsize
);
189 writebuf
= vmalloc(bufsize
);
190 offsets
= kmalloc_array(ebcnt
, sizeof(int), GFP_KERNEL
);
191 if (!readbuf
|| !writebuf
|| !offsets
)
193 for (i
= 0; i
< ebcnt
; i
++)
194 offsets
[i
] = mtd
->erasesize
;
195 prandom_bytes(writebuf
, bufsize
);
197 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
200 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
205 pr_info("doing operations\n");
206 for (op
= 0; op
< count
; op
++) {
207 if ((op
& 1023) == 0)
208 pr_info("%d operations done\n", op
);
209 err
= do_operation();
213 err
= mtdtest_relax();
217 pr_info("finished, %d operations done\n", op
);
227 pr_info("error %d occurred\n", err
);
228 printk(KERN_INFO
"=================================================\n");
231 module_init(mtd_stresstest_init
);
233 static void __exit
mtd_stresstest_exit(void)
237 module_exit(mtd_stresstest_exit
);
239 MODULE_DESCRIPTION("Stress test module");
240 MODULE_AUTHOR("Adrian Hunter");
241 MODULE_LICENSE("GPL");