]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/mtd/tests/mtd_stresstest.c
mtd: tests: don't print error messages when out-of-memory
[mirror_ubuntu-zesty-kernel.git] / drivers / mtd / tests / mtd_stresstest.c
CommitLineData
7163cea1
AB
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
ae0086cf
VN
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
7163cea1
AB
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/err.h>
28#include <linux/mtd/mtd.h>
5a0e3ad6 29#include <linux/slab.h>
7163cea1
AB
30#include <linux/sched.h>
31#include <linux/vmalloc.h>
bfea1d4e 32#include <linux/random.h>
7163cea1 33
7406060e 34static int dev = -EINVAL;
7163cea1
AB
35module_param(dev, int, S_IRUGO);
36MODULE_PARM_DESC(dev, "MTD device number to use");
37
38static int count = 10000;
39module_param(count, int, S_IRUGO);
40MODULE_PARM_DESC(count, "Number of operations to do (default is 10000)");
41
42static struct mtd_info *mtd;
43static unsigned char *writebuf;
44static unsigned char *readbuf;
45static unsigned char *bbt;
46static int *offsets;
47
48static int pgsize;
49static int bufsize;
50static int ebcnt;
51static int pgcnt;
7163cea1
AB
52
53static int rand_eb(void)
54{
bfea1d4e 55 unsigned int eb;
7163cea1
AB
56
57again:
aca662a3 58 eb = prandom_u32();
7163cea1
AB
59 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
60 eb %= (ebcnt - 1);
61 if (bbt[eb])
62 goto again;
63 return eb;
64}
65
66static int rand_offs(void)
67{
bfea1d4e 68 unsigned int offs;
7163cea1 69
aca662a3 70 offs = prandom_u32();
7163cea1
AB
71 offs %= bufsize;
72 return offs;
73}
74
75static int rand_len(int offs)
76{
bfea1d4e 77 unsigned int len;
7163cea1 78
aca662a3 79 len = prandom_u32();
7163cea1
AB
80 len %= (bufsize - offs);
81 return len;
82}
83
84static int erase_eraseblock(int ebnum)
85{
86 int err;
87 struct erase_info ei;
88 loff_t addr = ebnum * mtd->erasesize;
89
90 memset(&ei, 0, sizeof(struct erase_info));
91 ei.mtd = mtd;
92 ei.addr = addr;
93 ei.len = mtd->erasesize;
94
7e1f0dc0 95 err = mtd_erase(mtd, &ei);
7163cea1 96 if (unlikely(err)) {
ae0086cf 97 pr_err("error %d while erasing EB %d\n", err, ebnum);
7163cea1
AB
98 return err;
99 }
100
101 if (unlikely(ei.state == MTD_ERASE_FAILED)) {
ae0086cf 102 pr_err("some erase error occurred at EB %d\n",
7163cea1
AB
103 ebnum);
104 return -EIO;
105 }
106
107 return 0;
108}
109
110static int is_block_bad(int ebnum)
111{
112 loff_t addr = ebnum * mtd->erasesize;
113 int ret;
114
7086c19d 115 ret = mtd_block_isbad(mtd, addr);
7163cea1 116 if (ret)
ae0086cf 117 pr_info("block %d is bad\n", ebnum);
7163cea1
AB
118 return ret;
119}
120
121static int do_read(void)
122{
30fa9848 123 size_t read;
7163cea1
AB
124 int eb = rand_eb();
125 int offs = rand_offs();
126 int len = rand_len(offs), err;
127 loff_t addr;
128
129 if (bbt[eb + 1]) {
130 if (offs >= mtd->erasesize)
131 offs -= mtd->erasesize;
132 if (offs + len > mtd->erasesize)
133 len = mtd->erasesize - offs;
134 }
135 addr = eb * mtd->erasesize + offs;
329ad399 136 err = mtd_read(mtd, addr, len, &read, readbuf);
d57f4054 137 if (mtd_is_bitflip(err))
7163cea1
AB
138 err = 0;
139 if (unlikely(err || read != len)) {
ae0086cf 140 pr_err("error: read failed at 0x%llx\n",
7163cea1
AB
141 (long long)addr);
142 if (!err)
143 err = -EINVAL;
144 return err;
145 }
146 return 0;
147}
148
149static int do_write(void)
150{
151 int eb = rand_eb(), offs, err, len;
30fa9848 152 size_t written;
7163cea1
AB
153 loff_t addr;
154
155 offs = offsets[eb];
156 if (offs >= mtd->erasesize) {
157 err = erase_eraseblock(eb);
158 if (err)
159 return err;
160 offs = offsets[eb] = 0;
161 }
162 len = rand_len(offs);
163 len = ((len + pgsize - 1) / pgsize) * pgsize;
164 if (offs + len > mtd->erasesize) {
165 if (bbt[eb + 1])
166 len = mtd->erasesize - offs;
167 else {
168 err = erase_eraseblock(eb + 1);
169 if (err)
170 return err;
171 offsets[eb + 1] = 0;
172 }
173 }
174 addr = eb * mtd->erasesize + offs;
eda95cbf 175 err = mtd_write(mtd, addr, len, &written, writebuf);
7163cea1 176 if (unlikely(err || written != len)) {
ae0086cf 177 pr_err("error: write failed at 0x%llx\n",
7163cea1
AB
178 (long long)addr);
179 if (!err)
180 err = -EINVAL;
181 return err;
182 }
183 offs += len;
184 while (offs > mtd->erasesize) {
185 offsets[eb++] = mtd->erasesize;
186 offs -= mtd->erasesize;
187 }
188 offsets[eb] = offs;
189 return 0;
190}
191
192static int do_operation(void)
193{
aca662a3 194 if (prandom_u32() & 1)
7163cea1
AB
195 return do_read();
196 else
197 return do_write();
198}
199
200static int scan_for_bad_eraseblocks(void)
201{
202 int i, bad = 0;
203
2bfefa4c 204 bbt = kzalloc(ebcnt, GFP_KERNEL);
33777e66 205 if (!bbt)
7163cea1 206 return -ENOMEM;
7163cea1 207
8f461a73 208 if (!mtd_can_have_bb(mtd))
f5e2bae0
MTS
209 return 0;
210
ae0086cf 211 pr_info("scanning for bad eraseblocks\n");
7163cea1
AB
212 for (i = 0; i < ebcnt; ++i) {
213 bbt[i] = is_block_bad(i) ? 1 : 0;
214 if (bbt[i])
215 bad += 1;
216 cond_resched();
217 }
ae0086cf 218 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
7163cea1
AB
219 return 0;
220}
221
222static int __init mtd_stresstest_init(void)
223{
224 int err;
225 int i, op;
226 uint64_t tmp;
227
228 printk(KERN_INFO "\n");
229 printk(KERN_INFO "=================================================\n");
7406060e
WS
230
231 if (dev < 0) {
064a7694 232 pr_info("Please specify a valid mtd-device via module parameter\n");
ae0086cf 233 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
7406060e
WS
234 return -EINVAL;
235 }
236
ae0086cf 237 pr_info("MTD device: %d\n", dev);
7163cea1
AB
238
239 mtd = get_mtd_device(NULL, dev);
240 if (IS_ERR(mtd)) {
241 err = PTR_ERR(mtd);
ae0086cf 242 pr_err("error: cannot get MTD device\n");
7163cea1
AB
243 return err;
244 }
245
246 if (mtd->writesize == 1) {
ae0086cf 247 pr_info("not NAND flash, assume page size is 512 "
7163cea1
AB
248 "bytes.\n");
249 pgsize = 512;
250 } else
251 pgsize = mtd->writesize;
252
253 tmp = mtd->size;
254 do_div(tmp, mtd->erasesize);
255 ebcnt = tmp;
f5e2bae0 256 pgcnt = mtd->erasesize / pgsize;
7163cea1 257
ae0086cf 258 pr_info("MTD device size %llu, eraseblock size %u, "
7163cea1
AB
259 "page size %u, count of eraseblocks %u, pages per "
260 "eraseblock %u, OOB size %u\n",
261 (unsigned long long)mtd->size, mtd->erasesize,
262 pgsize, ebcnt, pgcnt, mtd->oobsize);
263
2f4478cc 264 if (ebcnt < 2) {
ae0086cf 265 pr_err("error: need at least 2 eraseblocks\n");
2f4478cc
WS
266 err = -ENOSPC;
267 goto out_put_mtd;
268 }
269
7163cea1
AB
270 /* Read or write up 2 eraseblocks at a time */
271 bufsize = mtd->erasesize * 2;
272
273 err = -ENOMEM;
274 readbuf = vmalloc(bufsize);
275 writebuf = vmalloc(bufsize);
276 offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL);
33777e66 277 if (!readbuf || !writebuf || !offsets)
7163cea1 278 goto out;
7163cea1
AB
279 for (i = 0; i < ebcnt; i++)
280 offsets[i] = mtd->erasesize;
4debec7a 281 prandom_bytes(writebuf, bufsize);
7163cea1
AB
282
283 err = scan_for_bad_eraseblocks();
284 if (err)
285 goto out;
286
287 /* Do operations */
ae0086cf 288 pr_info("doing operations\n");
7163cea1
AB
289 for (op = 0; op < count; op++) {
290 if ((op & 1023) == 0)
ae0086cf 291 pr_info("%d operations done\n", op);
7163cea1
AB
292 err = do_operation();
293 if (err)
294 goto out;
295 cond_resched();
296 }
ae0086cf 297 pr_info("finished, %d operations done\n", op);
7163cea1
AB
298
299out:
300 kfree(offsets);
301 kfree(bbt);
302 vfree(writebuf);
303 vfree(readbuf);
2f4478cc 304out_put_mtd:
7163cea1
AB
305 put_mtd_device(mtd);
306 if (err)
ae0086cf 307 pr_info("error %d occurred\n", err);
7163cea1
AB
308 printk(KERN_INFO "=================================================\n");
309 return err;
310}
311module_init(mtd_stresstest_init);
312
313static void __exit mtd_stresstest_exit(void)
314{
315 return;
316}
317module_exit(mtd_stresstest_exit);
318
319MODULE_DESCRIPTION("Stress test module");
320MODULE_AUTHOR("Adrian Hunter");
321MODULE_LICENSE("GPL");