Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * asynchronous raid6 recovery self test
4 * Copyright (c) 2009, Intel Corporation.
5 *
6 * based on drivers/md/raid6test/test.c:
7 * Copyright 2002-2007 H. Peter Anvin
8 */
9#include <linux/async_tx.h>
10#include <linux/gfp.h>
11#include <linux/mm.h>
12#include <linux/random.h>
13#include <linux/module.h>
14
15#undef pr
16#define pr(fmt, args...) pr_info("raid6test: " fmt, ##args)
17
18#define NDISKS 64 /* Including P and Q */
19
20static struct page *dataptrs[NDISKS];
21unsigned int dataoffs[NDISKS];
22static addr_conv_t addr_conv[NDISKS];
23static struct page *data[NDISKS+3];
24static struct page *spare;
25static struct page *recovi;
26static struct page *recovj;
27
28static void callback(void *param)
29{
30 struct completion *cmp = param;
31
32 complete(cmp);
33}
34
35static void makedata(int disks)
36{
37 int i;
38
39 for (i = 0; i < disks; i++) {
40 get_random_bytes(page_address(data[i]), PAGE_SIZE);
41 dataptrs[i] = data[i];
42 dataoffs[i] = 0;
43 }
44}
45
46static char disk_type(int d, int disks)
47{
48 if (d == disks - 2)
49 return 'P';
50 else if (d == disks - 1)
51 return 'Q';
52 else
53 return 'D';
54}
55
56/* Recover two failed blocks. */
57static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb,
58 struct page **ptrs, unsigned int *offs)
59{
60 struct async_submit_ctl submit;
61 struct completion cmp;
62 struct dma_async_tx_descriptor *tx = NULL;
63 enum sum_check_flags result = ~0;
64
65 if (faila > failb)
66 swap(faila, failb);
67
68 if (failb == disks-1) {
69 if (faila == disks-2) {
70 /* P+Q failure. Just rebuild the syndrome. */
71 init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
72 tx = async_gen_syndrome(ptrs, offs,
73 disks, bytes, &submit);
74 } else {
75 struct page *blocks[NDISKS];
76 struct page *dest;
77 int count = 0;
78 int i;
79
80 BUG_ON(disks > NDISKS);
81
82 /* data+Q failure. Reconstruct data from P,
83 * then rebuild syndrome
84 */
85 for (i = disks; i-- ; ) {
86 if (i == faila || i == failb)
87 continue;
88 blocks[count++] = ptrs[i];
89 }
90 dest = ptrs[faila];
91 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
92 NULL, NULL, addr_conv);
93 tx = async_xor(dest, blocks, 0, count, bytes, &submit);
94
95 init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
96 tx = async_gen_syndrome(ptrs, offs,
97 disks, bytes, &submit);
98 }
99 } else {
100 if (failb == disks-2) {
101 /* data+P failure. */
102 init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
103 tx = async_raid6_datap_recov(disks, bytes,
104 faila, ptrs, offs, &submit);
105 } else {
106 /* data+data failure. */
107 init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
108 tx = async_raid6_2data_recov(disks, bytes,
109 faila, failb, ptrs, offs, &submit);
110 }
111 }
112 init_completion(&cmp);
113 init_async_submit(&submit, ASYNC_TX_ACK, tx, callback, &cmp, addr_conv);
114 tx = async_syndrome_val(ptrs, offs,
115 disks, bytes, &result, spare, 0, &submit);
116 async_tx_issue_pending(tx);
117
118 if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0)
119 pr("%s: timeout! (faila: %d failb: %d disks: %d)\n",
120 __func__, faila, failb, disks);
121
122 if (result != 0)
123 pr("%s: validation failure! faila: %d failb: %d sum_check_flags: %x\n",
124 __func__, faila, failb, result);
125}
126
127static int test_disks(int i, int j, int disks)
128{
129 int erra, errb;
130
131 memset(page_address(recovi), 0xf0, PAGE_SIZE);
132 memset(page_address(recovj), 0xba, PAGE_SIZE);
133
134 dataptrs[i] = recovi;
135 dataptrs[j] = recovj;
136
137 raid6_dual_recov(disks, PAGE_SIZE, i, j, dataptrs, dataoffs);
138
139 erra = memcmp(page_address(data[i]), page_address(recovi), PAGE_SIZE);
140 errb = memcmp(page_address(data[j]), page_address(recovj), PAGE_SIZE);
141
142 pr("%s(%d, %d): faila=%3d(%c) failb=%3d(%c) %s\n",
143 __func__, i, j, i, disk_type(i, disks), j, disk_type(j, disks),
144 (!erra && !errb) ? "OK" : !erra ? "ERRB" : !errb ? "ERRA" : "ERRAB");
145
146 dataptrs[i] = data[i];
147 dataptrs[j] = data[j];
148
149 return erra || errb;
150}
151
152static int test(int disks, int *tests)
153{
154 struct dma_async_tx_descriptor *tx;
155 struct async_submit_ctl submit;
156 struct completion cmp;
157 int err = 0;
158 int i, j;
159
160 recovi = data[disks];
161 recovj = data[disks+1];
162 spare = data[disks+2];
163
164 makedata(disks);
165
166 /* Nuke syndromes */
167 memset(page_address(data[disks-2]), 0xee, PAGE_SIZE);
168 memset(page_address(data[disks-1]), 0xee, PAGE_SIZE);
169
170 /* Generate assumed good syndrome */
171 init_completion(&cmp);
172 init_async_submit(&submit, ASYNC_TX_ACK, NULL, callback, &cmp, addr_conv);
173 tx = async_gen_syndrome(dataptrs, dataoffs, disks, PAGE_SIZE, &submit);
174 async_tx_issue_pending(tx);
175
176 if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0) {
177 pr("error: initial gen_syndrome(%d) timed out\n", disks);
178 return 1;
179 }
180
181 pr("testing the %d-disk case...\n", disks);
182 for (i = 0; i < disks-1; i++)
183 for (j = i+1; j < disks; j++) {
184 (*tests)++;
185 err += test_disks(i, j, disks);
186 }
187
188 return err;
189}
190
191
192static int __init raid6_test(void)
193{
194 int err = 0;
195 int tests = 0;
196 int i;
197
198 for (i = 0; i < NDISKS+3; i++) {
199 data[i] = alloc_page(GFP_KERNEL);
200 if (!data[i]) {
201 while (i--)
202 put_page(data[i]);
203 return -ENOMEM;
204 }
205 }
206
207 /* the 4-disk and 5-disk cases are special for the recovery code */
208 if (NDISKS > 4)
209 err += test(4, &tests);
210 if (NDISKS > 5)
211 err += test(5, &tests);
212 /* the 11 and 12 disk cases are special for ioatdma (p-disabled
213 * q-continuation without extended descriptor)
214 */
215 if (NDISKS > 12) {
216 err += test(11, &tests);
217 err += test(12, &tests);
218 }
219
220 /* the 24 disk case is special for ioatdma as it is the boundary point
221 * at which it needs to switch from 8-source ops to 16-source
222 * ops for continuation (assumes DMA_HAS_PQ_CONTINUE is not set)
223 */
224 if (NDISKS > 24)
225 err += test(24, &tests);
226
227 err += test(NDISKS, &tests);
228
229 pr("\n");
230 pr("complete (%d tests, %d failure%s)\n",
231 tests, err, err == 1 ? "" : "s");
232
233 for (i = 0; i < NDISKS+3; i++)
234 put_page(data[i]);
235
236 return 0;
237}
238
239static void __exit raid6_test_exit(void)
240{
241}
242
243/* when compiled-in wait for drivers to load first (assumes dma drivers
244 * are also compiled-in)
245 */
246late_initcall(raid6_test);
247module_exit(raid6_test_exit);
248MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
249MODULE_DESCRIPTION("asynchronous RAID-6 recovery self tests");
250MODULE_LICENSE("GPL");
1/*
2 * asynchronous raid6 recovery self test
3 * Copyright (c) 2009, Intel Corporation.
4 *
5 * based on drivers/md/raid6test/test.c:
6 * Copyright 2002-2007 H. Peter Anvin
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22#include <linux/async_tx.h>
23#include <linux/gfp.h>
24#include <linux/mm.h>
25#include <linux/random.h>
26
27#undef pr
28#define pr(fmt, args...) pr_info("raid6test: " fmt, ##args)
29
30#define NDISKS 16 /* Including P and Q */
31
32static struct page *dataptrs[NDISKS];
33static addr_conv_t addr_conv[NDISKS];
34static struct page *data[NDISKS+3];
35static struct page *spare;
36static struct page *recovi;
37static struct page *recovj;
38
39static void callback(void *param)
40{
41 struct completion *cmp = param;
42
43 complete(cmp);
44}
45
46static void makedata(int disks)
47{
48 int i, j;
49
50 for (i = 0; i < disks; i++) {
51 for (j = 0; j < PAGE_SIZE/sizeof(u32); j += sizeof(u32)) {
52 u32 *p = page_address(data[i]) + j;
53
54 *p = random32();
55 }
56
57 dataptrs[i] = data[i];
58 }
59}
60
61static char disk_type(int d, int disks)
62{
63 if (d == disks - 2)
64 return 'P';
65 else if (d == disks - 1)
66 return 'Q';
67 else
68 return 'D';
69}
70
71/* Recover two failed blocks. */
72static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, struct page **ptrs)
73{
74 struct async_submit_ctl submit;
75 struct completion cmp;
76 struct dma_async_tx_descriptor *tx = NULL;
77 enum sum_check_flags result = ~0;
78
79 if (faila > failb)
80 swap(faila, failb);
81
82 if (failb == disks-1) {
83 if (faila == disks-2) {
84 /* P+Q failure. Just rebuild the syndrome. */
85 init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
86 tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
87 } else {
88 struct page *blocks[disks];
89 struct page *dest;
90 int count = 0;
91 int i;
92
93 /* data+Q failure. Reconstruct data from P,
94 * then rebuild syndrome
95 */
96 for (i = disks; i-- ; ) {
97 if (i == faila || i == failb)
98 continue;
99 blocks[count++] = ptrs[i];
100 }
101 dest = ptrs[faila];
102 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
103 NULL, NULL, addr_conv);
104 tx = async_xor(dest, blocks, 0, count, bytes, &submit);
105
106 init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
107 tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
108 }
109 } else {
110 if (failb == disks-2) {
111 /* data+P failure. */
112 init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
113 tx = async_raid6_datap_recov(disks, bytes, faila, ptrs, &submit);
114 } else {
115 /* data+data failure. */
116 init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
117 tx = async_raid6_2data_recov(disks, bytes, faila, failb, ptrs, &submit);
118 }
119 }
120 init_completion(&cmp);
121 init_async_submit(&submit, ASYNC_TX_ACK, tx, callback, &cmp, addr_conv);
122 tx = async_syndrome_val(ptrs, 0, disks, bytes, &result, spare, &submit);
123 async_tx_issue_pending(tx);
124
125 if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0)
126 pr("%s: timeout! (faila: %d failb: %d disks: %d)\n",
127 __func__, faila, failb, disks);
128
129 if (result != 0)
130 pr("%s: validation failure! faila: %d failb: %d sum_check_flags: %x\n",
131 __func__, faila, failb, result);
132}
133
134static int test_disks(int i, int j, int disks)
135{
136 int erra, errb;
137
138 memset(page_address(recovi), 0xf0, PAGE_SIZE);
139 memset(page_address(recovj), 0xba, PAGE_SIZE);
140
141 dataptrs[i] = recovi;
142 dataptrs[j] = recovj;
143
144 raid6_dual_recov(disks, PAGE_SIZE, i, j, dataptrs);
145
146 erra = memcmp(page_address(data[i]), page_address(recovi), PAGE_SIZE);
147 errb = memcmp(page_address(data[j]), page_address(recovj), PAGE_SIZE);
148
149 pr("%s(%d, %d): faila=%3d(%c) failb=%3d(%c) %s\n",
150 __func__, i, j, i, disk_type(i, disks), j, disk_type(j, disks),
151 (!erra && !errb) ? "OK" : !erra ? "ERRB" : !errb ? "ERRA" : "ERRAB");
152
153 dataptrs[i] = data[i];
154 dataptrs[j] = data[j];
155
156 return erra || errb;
157}
158
159static int test(int disks, int *tests)
160{
161 struct dma_async_tx_descriptor *tx;
162 struct async_submit_ctl submit;
163 struct completion cmp;
164 int err = 0;
165 int i, j;
166
167 recovi = data[disks];
168 recovj = data[disks+1];
169 spare = data[disks+2];
170
171 makedata(disks);
172
173 /* Nuke syndromes */
174 memset(page_address(data[disks-2]), 0xee, PAGE_SIZE);
175 memset(page_address(data[disks-1]), 0xee, PAGE_SIZE);
176
177 /* Generate assumed good syndrome */
178 init_completion(&cmp);
179 init_async_submit(&submit, ASYNC_TX_ACK, NULL, callback, &cmp, addr_conv);
180 tx = async_gen_syndrome(dataptrs, 0, disks, PAGE_SIZE, &submit);
181 async_tx_issue_pending(tx);
182
183 if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0) {
184 pr("error: initial gen_syndrome(%d) timed out\n", disks);
185 return 1;
186 }
187
188 pr("testing the %d-disk case...\n", disks);
189 for (i = 0; i < disks-1; i++)
190 for (j = i+1; j < disks; j++) {
191 (*tests)++;
192 err += test_disks(i, j, disks);
193 }
194
195 return err;
196}
197
198
199static int raid6_test(void)
200{
201 int err = 0;
202 int tests = 0;
203 int i;
204
205 for (i = 0; i < NDISKS+3; i++) {
206 data[i] = alloc_page(GFP_KERNEL);
207 if (!data[i]) {
208 while (i--)
209 put_page(data[i]);
210 return -ENOMEM;
211 }
212 }
213
214 /* the 4-disk and 5-disk cases are special for the recovery code */
215 if (NDISKS > 4)
216 err += test(4, &tests);
217 if (NDISKS > 5)
218 err += test(5, &tests);
219 /* the 11 and 12 disk cases are special for ioatdma (p-disabled
220 * q-continuation without extended descriptor)
221 */
222 if (NDISKS > 12) {
223 err += test(11, &tests);
224 err += test(12, &tests);
225 }
226 err += test(NDISKS, &tests);
227
228 pr("\n");
229 pr("complete (%d tests, %d failure%s)\n",
230 tests, err, err == 1 ? "" : "s");
231
232 for (i = 0; i < NDISKS+3; i++)
233 put_page(data[i]);
234
235 return 0;
236}
237
238static void raid6_test_exit(void)
239{
240}
241
242/* when compiled-in wait for drivers to load first (assumes dma drivers
243 * are also compliled-in)
244 */
245late_initcall(raid6_test);
246module_exit(raid6_test_exit);
247MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
248MODULE_DESCRIPTION("asynchronous RAID-6 recovery self tests");
249MODULE_LICENSE("GPL");