blob: 8062098930d6cda43086ca43ef1c00a124f10b13 [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:
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020048 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
Jason A. Donenfeld8032bf12022-10-09 20:44:02 -060049 eb = get_random_u32_below(ebcnt - 1);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020050 if (bbt[eb])
51 goto again;
52 return eb;
53}
54
55static int rand_offs(void)
56{
Jason A. Donenfeld8032bf12022-10-09 20:44:02 -060057 return get_random_u32_below(bufsize);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020058}
59
60static int rand_len(int offs)
61{
Jason A. Donenfeld8032bf12022-10-09 20:44:02 -060062 return get_random_u32_below(bufsize - offs);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020063}
64
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020065static int do_read(void)
66{
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020067 int eb = rand_eb();
68 int offs = rand_offs();
Akinobu Mitaabc173a2013-08-15 22:55:08 +090069 int len = rand_len(offs);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020070 loff_t addr;
71
72 if (bbt[eb + 1]) {
73 if (offs >= mtd->erasesize)
74 offs -= mtd->erasesize;
75 if (offs + len > mtd->erasesize)
76 len = mtd->erasesize - offs;
77 }
Brian Norrisb9da8ba2015-02-28 02:02:26 -080078 addr = (loff_t)eb * mtd->erasesize + offs;
Akinobu Mitaabc173a2013-08-15 22:55:08 +090079 return mtdtest_read(mtd, addr, len, readbuf);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020080}
81
82static int do_write(void)
83{
84 int eb = rand_eb(), offs, err, len;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020085 loff_t addr;
86
87 offs = offsets[eb];
88 if (offs >= mtd->erasesize) {
Akinobu Mita5a78df62013-08-03 18:52:13 +090089 err = mtdtest_erase_eraseblock(mtd, eb);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +020090 if (err)
91 return err;
92 offs = offsets[eb] = 0;
93 }
94 len = rand_len(offs);
95 len = ((len + pgsize - 1) / pgsize) * pgsize;
96 if (offs + len > mtd->erasesize) {
97 if (bbt[eb + 1])
98 len = mtd->erasesize - offs;
99 else {
Akinobu Mita5a78df62013-08-03 18:52:13 +0900100 err = mtdtest_erase_eraseblock(mtd, eb + 1);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200101 if (err)
102 return err;
103 offsets[eb + 1] = 0;
104 }
105 }
Brian Norrisb9da8ba2015-02-28 02:02:26 -0800106 addr = (loff_t)eb * mtd->erasesize + offs;
Akinobu Mita5a78df62013-08-03 18:52:13 +0900107 err = mtdtest_write(mtd, addr, len, writebuf);
Akinobu Mita8a9f4aa2013-08-15 22:55:09 +0900108 if (unlikely(err))
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200109 return err;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200110 offs += len;
111 while (offs > mtd->erasesize) {
112 offsets[eb++] = mtd->erasesize;
113 offs -= mtd->erasesize;
114 }
115 offsets[eb] = offs;
116 return 0;
117}
118
119static int do_operation(void)
120{
Jason A. Donenfeld8032bf12022-10-09 20:44:02 -0600121 if (get_random_u32_below(2))
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200122 return do_read();
123 else
124 return do_write();
125}
126
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200127static int __init mtd_stresstest_init(void)
128{
129 int err;
130 int i, op;
131 uint64_t tmp;
132
133 printk(KERN_INFO "\n");
134 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200135
136 if (dev < 0) {
Masanari Iida064a76942012-11-09 23:20:58 +0900137 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayananae0086c2012-10-10 23:10:22 +0530138 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200139 return -EINVAL;
140 }
141
Vikram Narayananae0086c2012-10-10 23:10:22 +0530142 pr_info("MTD device: %d\n", dev);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200143
144 mtd = get_mtd_device(NULL, dev);
145 if (IS_ERR(mtd)) {
146 err = PTR_ERR(mtd);
Vikram Narayananae0086c2012-10-10 23:10:22 +0530147 pr_err("error: cannot get MTD device\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200148 return err;
149 }
150
151 if (mtd->writesize == 1) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530152 pr_info("not NAND flash, assume page size is 512 "
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200153 "bytes.\n");
154 pgsize = 512;
155 } else
156 pgsize = mtd->writesize;
157
158 tmp = mtd->size;
159 do_div(tmp, mtd->erasesize);
160 ebcnt = tmp;
Morten Thunberg Svendsenf5e2bae2010-01-06 10:48:18 +0100161 pgcnt = mtd->erasesize / pgsize;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200162
Vikram Narayananae0086c2012-10-10 23:10:22 +0530163 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200164 "page size %u, count of eraseblocks %u, pages per "
165 "eraseblock %u, OOB size %u\n",
166 (unsigned long long)mtd->size, mtd->erasesize,
167 pgsize, ebcnt, pgcnt, mtd->oobsize);
168
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100169 if (ebcnt < 2) {
Vikram Narayananae0086c2012-10-10 23:10:22 +0530170 pr_err("error: need at least 2 eraseblocks\n");
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100171 err = -ENOSPC;
172 goto out_put_mtd;
173 }
174
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200175 /* Read or write up 2 eraseblocks at a time */
176 bufsize = mtd->erasesize * 2;
177
178 err = -ENOMEM;
179 readbuf = vmalloc(bufsize);
180 writebuf = vmalloc(bufsize);
Kees Cook6da2ec52018-06-12 13:55:00 -0700181 offsets = kmalloc_array(ebcnt, sizeof(int), GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700182 if (!readbuf || !writebuf || !offsets)
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200183 goto out;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200184 for (i = 0; i < ebcnt; i++)
185 offsets[i] = mtd->erasesize;
Jason A. Donenfeld197173d2022-10-05 17:49:46 +0200186 get_random_bytes(writebuf, bufsize);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200187
Akinobu Mita5a78df62013-08-03 18:52:13 +0900188 bbt = kzalloc(ebcnt, GFP_KERNEL);
189 if (!bbt)
190 goto out;
191 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200192 if (err)
193 goto out;
194
195 /* Do operations */
Vikram Narayananae0086c2012-10-10 23:10:22 +0530196 pr_info("doing operations\n");
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200197 for (op = 0; op < count; op++) {
198 if ((op & 1023) == 0)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530199 pr_info("%d operations done\n", op);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200200 err = do_operation();
201 if (err)
202 goto out;
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200203
204 err = mtdtest_relax();
205 if (err)
206 goto out;
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200207 }
Vikram Narayananae0086c2012-10-10 23:10:22 +0530208 pr_info("finished, %d operations done\n", op);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200209
210out:
211 kfree(offsets);
212 kfree(bbt);
213 vfree(writebuf);
214 vfree(readbuf);
Wolfram Sang2f4478c2011-11-29 15:34:08 +0100215out_put_mtd:
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200216 put_mtd_device(mtd);
217 if (err)
Vikram Narayananae0086c2012-10-10 23:10:22 +0530218 pr_info("error %d occurred\n", err);
Artem Bityutskiy7163cea2008-12-08 13:36:47 +0200219 printk(KERN_INFO "=================================================\n");
220 return err;
221}
222module_init(mtd_stresstest_init);
223
224static void __exit mtd_stresstest_exit(void)
225{
226 return;
227}
228module_exit(mtd_stresstest_exit);
229
230MODULE_DESCRIPTION("Stress test module");
231MODULE_AUTHOR("Adrian Hunter");
232MODULE_LICENSE("GPL");