blob: bf1b4765c8f68eeef6cdb0c0f50105f7fe8c885f [file] [log] [blame]
Thomas Gleixnerdd165a62019-05-20 19:08:13 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* -*- linux-c -*- ------------------------------------------------------- *
3 *
4 * Copyright 2002 H. Peter Anvin - All Rights Reserved
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * ----------------------------------------------------------------------- */
7
8/*
NeilBrowna8e026c2010-08-12 06:44:54 +10009 * raid6/algos.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Algorithm list and algorithm selection for RAID-6
12 */
13
Dan Williamsf701d582009-03-31 15:09:39 +110014#include <linux/raid/pq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#ifndef __KERNEL__
16#include <sys/mman.h>
H. Peter Anvind7e70ba2005-09-16 19:27:29 -070017#include <stdio.h>
Dan Williamsf701d582009-03-31 15:09:39 +110018#else
Jim Kukunasf674ef72012-05-22 13:54:16 +100019#include <linux/module.h>
NeilBrownd5302fe2010-08-12 06:38:24 +100020#include <linux/gfp.h>
Dan Williamsf701d582009-03-31 15:09:39 +110021#if !RAID6_USE_EMPTY_ZERO_PAGE
22/* In .bss so it's zeroed */
23const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
24EXPORT_SYMBOL(raid6_empty_zero_page);
25#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#endif
27
28struct raid6_calls raid6_call;
Dan Williamsf701d582009-03-31 15:09:39 +110029EXPORT_SYMBOL_GPL(raid6_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031const struct raid6_calls * const raid6_algos[] = {
Al Viroca5cd872007-10-29 04:31:16 +000032#if defined(__i386__) && !defined(__arch_um__)
Gayatri Kammelae0a491c2016-08-12 18:03:19 -070033#ifdef CONFIG_AS_AVX512
Gayatri Kammelae0a491c2016-08-12 18:03:19 -070034 &raid6_avx512x2,
Daniel Verkamp0437de42018-11-12 15:26:51 -080035 &raid6_avx512x1,
Gayatri Kammelae0a491c2016-08-12 18:03:19 -070036#endif
Daniel Verkamp0437de42018-11-12 15:26:51 -080037#ifdef CONFIG_AS_AVX2
38 &raid6_avx2x2,
39 &raid6_avx2x1,
40#endif
41 &raid6_sse2x2,
42 &raid6_sse2x1,
43 &raid6_sse1x2,
44 &raid6_sse1x1,
45 &raid6_mmxx2,
46 &raid6_mmxx1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
Al Viroca5cd872007-10-29 04:31:16 +000048#if defined(__x86_64__) && !defined(__arch_um__)
Gayatri Kammelae0a491c2016-08-12 18:03:19 -070049#ifdef CONFIG_AS_AVX512
Gayatri Kammelae0a491c2016-08-12 18:03:19 -070050 &raid6_avx512x4,
Daniel Verkamp0437de42018-11-12 15:26:51 -080051 &raid6_avx512x2,
52 &raid6_avx512x1,
Gayatri Kammelae0a491c2016-08-12 18:03:19 -070053#endif
Daniel Verkamp0437de42018-11-12 15:26:51 -080054#ifdef CONFIG_AS_AVX2
55 &raid6_avx2x4,
56 &raid6_avx2x2,
57 &raid6_avx2x1,
58#endif
59 &raid6_sse2x4,
60 &raid6_sse2x2,
61 &raid6_sse2x1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63#ifdef CONFIG_ALTIVEC
Matt Brown751ba792017-08-04 13:42:32 +100064 &raid6_vpermxor8,
Daniel Verkamp0437de42018-11-12 15:26:51 -080065 &raid6_vpermxor4,
66 &raid6_vpermxor2,
67 &raid6_vpermxor1,
68 &raid6_altivec8,
69 &raid6_altivec4,
70 &raid6_altivec2,
71 &raid6_altivec1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#endif
Martin Schwidefsky474fd6e2016-08-23 13:30:24 +020073#if defined(CONFIG_S390)
74 &raid6_s390vx8,
75#endif
Ard Biesheuvel7d119652013-05-16 17:20:32 +020076#ifdef CONFIG_KERNEL_MODE_NEON
Ard Biesheuvel7d119652013-05-16 17:20:32 +020077 &raid6_neonx8,
Daniel Verkamp0437de42018-11-12 15:26:51 -080078 &raid6_neonx4,
79 &raid6_neonx2,
80 &raid6_neonx1,
Ard Biesheuvel7d119652013-05-16 17:20:32 +020081#endif
Daniel Verkamp0437de42018-11-12 15:26:51 -080082#if defined(__ia64__)
83 &raid6_intx32,
84 &raid6_intx16,
85#endif
86 &raid6_intx8,
87 &raid6_intx4,
88 &raid6_intx2,
89 &raid6_intx1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 NULL
91};
92
Jim Kukunas048a8b82012-05-22 13:54:18 +100093void (*raid6_2data_recov)(int, size_t, int, int, void **);
94EXPORT_SYMBOL_GPL(raid6_2data_recov);
95
96void (*raid6_datap_recov)(int, size_t, int, void **);
97EXPORT_SYMBOL_GPL(raid6_datap_recov);
98
99const struct raid6_recov_calls *const raid6_recov_algos[] = {
Gayatri Kammela13c520b2016-08-12 18:03:20 -0700100#ifdef CONFIG_AS_AVX512
101 &raid6_recov_avx512,
102#endif
Jim Kukunas70567412012-11-08 13:47:44 -0800103#ifdef CONFIG_AS_AVX2
104 &raid6_recov_avx2,
105#endif
Jan Beulich75aaf4c2015-01-23 08:29:50 +0000106#ifdef CONFIG_AS_SSSE3
Jim Kukunas048a8b82012-05-22 13:54:18 +1000107 &raid6_recov_ssse3,
108#endif
Martin Schwidefskyf5b55fa2016-08-31 09:27:35 +0200109#ifdef CONFIG_S390
110 &raid6_recov_s390xc,
111#endif
Ard Biesheuvel6ec4e2512017-07-13 18:16:01 +0100112#if defined(CONFIG_KERNEL_MODE_NEON)
113 &raid6_recov_neon,
114#endif
Jim Kukunas048a8b82012-05-22 13:54:18 +1000115 &raid6_recov_intx1,
116 NULL
117};
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#ifdef __KERNEL__
120#define RAID6_TIME_JIFFIES_LG2 4
121#else
122/* Need more time to be stable in userspace */
123#define RAID6_TIME_JIFFIES_LG2 9
Dan Williamsf701d582009-03-31 15:09:39 +1100124#define time_before(x, y) ((x) < (y))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#endif
126
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800127#define RAID6_TEST_DISKS 8
128#define RAID6_TEST_DISKS_ORDER 3
129
Jim Kukunas96e67702012-05-22 13:54:24 +1000130static inline const struct raid6_recov_calls *raid6_choose_recov(void)
Jim Kukunas048a8b82012-05-22 13:54:18 +1000131{
132 const struct raid6_recov_calls *const *algo;
133 const struct raid6_recov_calls *best;
134
135 for (best = NULL, algo = raid6_recov_algos; *algo; algo++)
136 if (!best || (*algo)->priority > best->priority)
137 if (!(*algo)->valid || (*algo)->valid())
138 best = *algo;
139
140 if (best) {
141 raid6_2data_recov = best->data2;
142 raid6_datap_recov = best->datap;
143
Anton Blanchardb395f752014-10-13 23:03:16 +1100144 pr_info("raid6: using %s recovery algorithm\n", best->name);
Jim Kukunas048a8b82012-05-22 13:54:18 +1000145 } else
Anton Blanchardb395f752014-10-13 23:03:16 +1100146 pr_err("raid6: Yikes! No recovery algorithm found!\n");
Jim Kukunas96e67702012-05-22 13:54:24 +1000147
148 return best;
Jim Kukunas048a8b82012-05-22 13:54:18 +1000149}
150
Jim Kukunas96e67702012-05-22 13:54:24 +1000151static inline const struct raid6_calls *raid6_choose_gen(
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800152 void *(*const dptrs)[RAID6_TEST_DISKS], const int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Markus Stockhausenfe5cbc62014-12-15 12:57:04 +1100154 unsigned long perf, bestgenperf, bestxorperf, j0, j1;
155 int start = (disks>>1)-1, stop = disks-3; /* work on the second half of the disks */
Jim Kukunas96e67702012-05-22 13:54:24 +1000156 const struct raid6_calls *const *algo;
157 const struct raid6_calls *best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Markus Stockhausenfe5cbc62014-12-15 12:57:04 +1100159 for (bestgenperf = 0, bestxorperf = 0, best = NULL, algo = raid6_algos; *algo; algo++) {
Jim Kukunas96e67702012-05-22 13:54:24 +1000160 if (!best || (*algo)->prefer >= best->prefer) {
161 if ((*algo)->valid && !(*algo)->valid())
162 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Daniel Verkampbe85f932018-11-12 15:26:52 -0800164 if (!IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK)) {
165 best = *algo;
166 break;
167 }
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 perf = 0;
170
171 preempt_disable();
172 j0 = jiffies;
Jim Kukunas96e67702012-05-22 13:54:24 +1000173 while ((j1 = jiffies) == j0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 cpu_relax();
Julia Lawall62b05592008-04-28 02:15:56 -0700175 while (time_before(jiffies,
176 j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
Jim Kukunas96e67702012-05-22 13:54:24 +1000177 (*algo)->gen_syndrome(disks, PAGE_SIZE, *dptrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 perf++;
179 }
180 preempt_enable();
181
Markus Stockhausenfe5cbc62014-12-15 12:57:04 +1100182 if (perf > bestgenperf) {
183 bestgenperf = perf;
Jim Kukunas96e67702012-05-22 13:54:24 +1000184 best = *algo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Markus Stockhausenfe5cbc62014-12-15 12:57:04 +1100186 pr_info("raid6: %-8s gen() %5ld MB/s\n", (*algo)->name,
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800187 (perf * HZ * (disks-2)) >>
188 (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2));
Markus Stockhausenfe5cbc62014-12-15 12:57:04 +1100189
190 if (!(*algo)->xor_syndrome)
191 continue;
192
193 perf = 0;
194
195 preempt_disable();
196 j0 = jiffies;
197 while ((j1 = jiffies) == j0)
198 cpu_relax();
199 while (time_before(jiffies,
200 j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
201 (*algo)->xor_syndrome(disks, start, stop,
202 PAGE_SIZE, *dptrs);
203 perf++;
204 }
205 preempt_enable();
206
207 if (best == *algo)
208 bestxorperf = perf;
209
210 pr_info("raid6: %-8s xor() %5ld MB/s\n", (*algo)->name,
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800211 (perf * HZ * (disks-2)) >>
212 (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2 + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 }
215
Adrian Bunka5d68392006-06-23 02:05:59 -0700216 if (best) {
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800217 if (IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK)) {
218 pr_info("raid6: using algorithm %s gen() %ld MB/s\n",
219 best->name,
220 (bestgenperf * HZ * (disks-2)) >>
221 (20 - PAGE_SHIFT+RAID6_TIME_JIFFIES_LG2));
222 if (best->xor_syndrome)
223 pr_info("raid6: .... xor() %ld MB/s, rmw enabled\n",
224 (bestxorperf * HZ * (disks-2)) >>
225 (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2 + 1));
226 } else
227 pr_info("raid6: skip pq benchmark and using algorithm %s\n",
228 best->name);
Adrian Bunka5d68392006-06-23 02:05:59 -0700229 raid6_call = *best;
230 } else
Anton Blanchardb395f752014-10-13 23:03:16 +1100231 pr_err("raid6: Yikes! No algorithm found!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jim Kukunas96e67702012-05-22 13:54:24 +1000233 return best;
234}
235
236
237/* Try to pick the best algorithm */
238/* This code uses the gfmul table as convenient data set to abuse */
239
240int __init raid6_select_algo(void)
241{
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800242 const int disks = RAID6_TEST_DISKS;
Jim Kukunas96e67702012-05-22 13:54:24 +1000243
244 const struct raid6_calls *gen_best;
245 const struct raid6_recov_calls *rec_best;
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800246 char *disk_ptr, *p;
247 void *dptrs[RAID6_TEST_DISKS];
248 int i, cycle;
Jim Kukunas96e67702012-05-22 13:54:24 +1000249
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800250 /* prepare the buffer and fill it circularly with gfmul table */
251 disk_ptr = (char *)__get_free_pages(GFP_KERNEL, RAID6_TEST_DISKS_ORDER);
252 if (!disk_ptr) {
Anton Blanchardb395f752014-10-13 23:03:16 +1100253 pr_err("raid6: Yikes! No memory available.\n");
Jim Kukunas96e67702012-05-22 13:54:24 +1000254 return -ENOMEM;
255 }
256
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800257 p = disk_ptr;
258 for (i = 0; i < disks; i++)
259 dptrs[i] = p + PAGE_SIZE * i;
260
261 cycle = ((disks - 2) * PAGE_SIZE) / 65536;
262 for (i = 0; i < cycle; i++) {
263 memcpy(p, raid6_gfmul, 65536);
264 p += 65536;
265 }
266
267 if ((disks - 2) * PAGE_SIZE % 65536)
268 memcpy(p, raid6_gfmul, (disks - 2) * PAGE_SIZE % 65536);
Jim Kukunas96e67702012-05-22 13:54:24 +1000269
270 /* select raid gen_syndrome function */
271 gen_best = raid6_choose_gen(&dptrs, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Jim Kukunas048a8b82012-05-22 13:54:18 +1000273 /* select raid recover functions */
Jim Kukunas96e67702012-05-22 13:54:24 +1000274 rec_best = raid6_choose_recov();
Jim Kukunas048a8b82012-05-22 13:54:18 +1000275
Zhengyuan Liuf591df32019-12-20 10:21:28 +0800276 free_pages((unsigned long)disk_ptr, RAID6_TEST_DISKS_ORDER);
Jim Kukunas96e67702012-05-22 13:54:24 +1000277
278 return gen_best && rec_best ? 0 : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
Dan Williamsf701d582009-03-31 15:09:39 +1100280
281static void raid6_exit(void)
282{
283 do { } while (0);
284}
285
286subsys_initcall(raid6_select_algo);
287module_exit(raid6_exit);
288MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100289MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");