blob: cb29c8c1b37033f118333121ab13d7abc9eea68c [file] [log] [blame]
Thomas Gleixner4cd10352019-05-29 16:57:41 -07001// SPDX-License-Identifier: GPL-2.0-only
Artem Bityutskiy7163cea2008-12-08 13:36:47 +02002/*
3 * Copyright (C) 2006-2008 Nokia Corporation
4 *
Artem Bityutskiy7163cea2008-12-08 13:36:47 +02005 * Test random reads, writes and erases on MTD device.
6 *
7 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
8 */
9
Vikram Narayananae0086c2012-10-10 23:10:22 +053010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020012#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020018#include <linux/sched.h>
19#include <linux/vmalloc.h>
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030020#include <linux/random.h>
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020021
Akinobu Mita5a78df62013-08-03 18:52:13 +090022#include "mtd_test.h"
23
Wolfram Sang74060602011-10-30 00:11:53 +020024static int dev = -EINVAL;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020025module_param(dev, int, S_IRUGO);
26MODULE_PARM_DESC(dev, "MTD device number to use");
27
28static int count = 10000;
29module_param(count, int, S_IRUGO);
30MODULE_PARM_DESC(count, "Number of operations to do (default is 10000)");
31
32static struct mtd_info *mtd;
33static unsigned char *writebuf;
34static unsigned char *readbuf;
35static unsigned char *bbt;
36static int *offsets;
37
38static int pgsize;
39static int bufsize;
40static int ebcnt;
41static int pgcnt;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020042
43static int rand_eb(void)
44{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030045 unsigned int eb;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020046
47again:
Akinobu Mitaaca662a2013-01-03 21:19:13 +090048 eb = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020049 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
50 eb %= (ebcnt - 1);
51 if (bbt[eb])
52 goto again;
53 return eb;
54}
55
56static int rand_offs(void)
57{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030058 unsigned int offs;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020059
Akinobu Mitaaca662a2013-01-03 21:19:13 +090060 offs = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020061 offs %= bufsize;
62 return offs;
63}
64
65static int rand_len(int offs)
66{
Artem Bityutskiybfea1d42012-05-18 18:44:53 +030067 unsigned int len;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020068
Akinobu Mitaaca662a2013-01-03 21:19:13 +090069 len = prandom_u32();
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020070 len %= (bufsize - offs);
71 return len;
72}
73
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020074static int do_read(void)
75{
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020076 int eb = rand_eb();
77 int offs = rand_offs();
Akinobu Mitaabc173a2013-08-15 22:55:08 +090078 int len = rand_len(offs);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020079 loff_t addr;
80
81 if (bbt[eb + 1]) {
82 if (offs >= mtd->erasesize)
83 offs -= mtd->erasesize;
84 if (offs + len > mtd->erasesize)
85 len = mtd->erasesize - offs;
86 }
Brian Norrisb9da8ba2015-02-28 02:02:26 -080087 addr = (loff_t)eb * mtd->erasesize + offs;
Akinobu Mitaabc173a2013-08-15 22:55:08 +090088 return mtdtest_read(mtd, addr, len, readbuf);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020089}
90
91static int do_write(void)
92{
93 int eb = rand_eb(), offs, err, len;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020094 loff_t addr;
95
96 offs = offsets[eb];
97 if (offs >= mtd->erasesize) {
Akinobu Mita5a78df62013-08-03 18:52:13 +090098 err = mtdtest_erase_eraseblock(mtd, eb);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020099 if (err)
100 return err;
101 offs = offsets[eb] = 0;
102 }
103 len = rand_len(offs);
104 len = ((len + pgsize - 1) / pgsize) * pgsize;
105 if (offs + len > mtd->erasesize) {
106 if (bbt[eb + 1])
107 len = mtd->erasesize - offs;
108 else {
Akinobu Mita5a78df62013-08-03 18:52:13 +0900109 err = mtdtest_erase_eraseblock(mtd, eb + 1);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200110 if (err)
111 return err;
112 offsets[eb + 1] = 0;
113 }
114 }
Brian Norrisb9da8ba2015-02-28 02:02:26 -0800115 addr = (loff_t)eb * mtd->erasesize + offs;
Akinobu Mita5a78df62013-08-03 18:52:13 +0900116 err = mtdtest_write(mtd, addr, len, writebuf);
Akinobu Mita8a9f4aa2013-08-15 22:55:09 +0900117 if (unlikely(err))
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200118 return err;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200119 offs += len;
120 while (offs > mtd->erasesize) {
121 offsets[eb++] = mtd->erasesize;
122 offs -= mtd->erasesize;
123 }
124 offsets[eb] = offs;
125 return 0;
126}
127
128static int do_operation(void)
129{
Akinobu Mitaaca662a2013-01-03 21:19:13 +0900130 if (prandom_u32() & 1)
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200131 return do_read();
132 else
133 return do_write();
134}
135
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200136static int __init mtd_stresstest_init(void)
137{
138 int err;
139 int i, op;
140 uint64_t tmp;
141
142 printk(KERN_INFO "\n");
143 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200144
145 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900146 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayananae0086c2012-10-10 23:10:22 +0530147 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200148 return -EINVAL;
149 }
150
Vikram Narayananae0086c2012-10-10 23:10:22 +0530151 pr_info("MTD device: %d\n", dev);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200152
153 mtd = get_mtd_device(NULL, dev);
154 if (IS_ERR(mtd)) {
155 err = PTR_ERR(mtd);
Vikram Narayananae0086c2012-10-10 23:10:22 +0530156 pr_err("error: cannot get MTD device\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200157 return err;
158 }
159
160 if (mtd->writesize == 1) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530161 pr_info("not NAND flash, assume page size is 512 "
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200162 "bytes.\n");
163 pgsize = 512;
164 } else
165 pgsize = mtd->writesize;
166
167 tmp = mtd->size;
168 do_div(tmp, mtd->erasesize);
169 ebcnt = tmp;
Morten Thunberg Svendsenf5e2bae2010-01-06 10:48:18 +0100170 pgcnt = mtd->erasesize / pgsize;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200171
Vikram Narayananae0086c2012-10-10 23:10:22 +0530172 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200173 "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);
177
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100178 if (ebcnt < 2) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530179 pr_err("error: need at least 2 eraseblocks\n");
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100180 err = -ENOSPC;
181 goto out_put_mtd;
182 }
183
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200184 /* Read or write up 2 eraseblocks at a time */
185 bufsize = mtd->erasesize * 2;
186
187 err = -ENOMEM;
188 readbuf = vmalloc(bufsize);
189 writebuf = vmalloc(bufsize);
Kees Cook6da2ec52018-06-12 13:55:00 -0700190 offsets = kmalloc_array(ebcnt, sizeof(int), GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700191 if (!readbuf || !writebuf || !offsets)
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200192 goto out;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200193 for (i = 0; i < ebcnt; i++)
194 offsets[i] = mtd->erasesize;
Akinobu Mita4debec72013-02-27 17:05:40 -0800195 prandom_bytes(writebuf, bufsize);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200196
Akinobu Mita5a78df62013-08-03 18:52:13 +0900197 bbt = kzalloc(ebcnt, GFP_KERNEL);
198 if (!bbt)
199 goto out;
200 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200201 if (err)
202 goto out;
203
204 /* Do operations */
Vikram Narayananae0086c2012-10-10 23:10:22 +0530205 pr_info("doing operations\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200206 for (op = 0; op < count; op++) {
207 if ((op & 1023) == 0)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530208 pr_info("%d operations done\n", op);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200209 err = do_operation();
210 if (err)
211 goto out;
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200212
213 err = mtdtest_relax();
214 if (err)
215 goto out;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200216 }
Vikram Narayananae0086c2012-10-10 23:10:22 +0530217 pr_info("finished, %d operations done\n", op);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200218
219out:
220 kfree(offsets);
221 kfree(bbt);
222 vfree(writebuf);
223 vfree(readbuf);
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100224out_put_mtd:
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200225 put_mtd_device(mtd);
226 if (err)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530227 pr_info("error %d occurred\n", err);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200228 printk(KERN_INFO "=================================================\n");
229 return err;
230}
231module_init(mtd_stresstest_init);
232
233static void __exit mtd_stresstest_exit(void)
234{
235 return;
236}
237module_exit(mtd_stresstest_exit);
238
239MODULE_DESCRIPTION("Stress test module");
240MODULE_AUTHOR("Adrian Hunter");
241MODULE_LICENSE("GPL");