xref: /linux/lib/raid6/algos.c (revision cfc4ca8986bb1f6182da6cd7bb57f228590b4643)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- linux-c -*- ------------------------------------------------------- *
3  *
4  *   Copyright 2002 H. Peter Anvin - All Rights Reserved
5  *
6  * ----------------------------------------------------------------------- */
7 
8 /*
9  * raid6/algos.c
10  *
11  * Algorithm list and algorithm selection for RAID-6
12  */
13 
14 #include <linux/raid/pq.h>
15 #ifndef __KERNEL__
16 #include <sys/mman.h>
17 #include <stdio.h>
18 #else
19 #include <linux/module.h>
20 #include <linux/gfp.h>
21 /* In .bss so it's zeroed */
22 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
23 EXPORT_SYMBOL(raid6_empty_zero_page);
24 #endif
25 
26 struct raid6_calls raid6_call;
27 EXPORT_SYMBOL_GPL(raid6_call);
28 
29 const struct raid6_calls * const raid6_algos[] = {
30 #if defined(__i386__) && !defined(__arch_um__)
31 	&raid6_avx512x2,
32 	&raid6_avx512x1,
33 	&raid6_avx2x2,
34 	&raid6_avx2x1,
35 	&raid6_sse2x2,
36 	&raid6_sse2x1,
37 	&raid6_sse1x2,
38 	&raid6_sse1x1,
39 	&raid6_mmxx2,
40 	&raid6_mmxx1,
41 #endif
42 #if defined(__x86_64__) && !defined(__arch_um__)
43 	&raid6_avx512x4,
44 	&raid6_avx512x2,
45 	&raid6_avx512x1,
46 	&raid6_avx2x4,
47 	&raid6_avx2x2,
48 	&raid6_avx2x1,
49 	&raid6_sse2x4,
50 	&raid6_sse2x2,
51 	&raid6_sse2x1,
52 #endif
53 #ifdef CONFIG_ALTIVEC
54 	&raid6_vpermxor8,
55 	&raid6_vpermxor4,
56 	&raid6_vpermxor2,
57 	&raid6_vpermxor1,
58 	&raid6_altivec8,
59 	&raid6_altivec4,
60 	&raid6_altivec2,
61 	&raid6_altivec1,
62 #endif
63 #if defined(CONFIG_S390)
64 	&raid6_s390vx8,
65 #endif
66 #ifdef CONFIG_KERNEL_MODE_NEON
67 	&raid6_neonx8,
68 	&raid6_neonx4,
69 	&raid6_neonx2,
70 	&raid6_neonx1,
71 #endif
72 #ifdef CONFIG_LOONGARCH
73 #ifdef CONFIG_CPU_HAS_LASX
74 	&raid6_lasx,
75 #endif
76 #ifdef CONFIG_CPU_HAS_LSX
77 	&raid6_lsx,
78 #endif
79 #endif
80 	&raid6_intx8,
81 	&raid6_intx4,
82 	&raid6_intx2,
83 	&raid6_intx1,
84 	NULL
85 };
86 
87 void (*raid6_2data_recov)(int, size_t, int, int, void **);
88 EXPORT_SYMBOL_GPL(raid6_2data_recov);
89 
90 void (*raid6_datap_recov)(int, size_t, int, void **);
91 EXPORT_SYMBOL_GPL(raid6_datap_recov);
92 
93 const struct raid6_recov_calls *const raid6_recov_algos[] = {
94 #ifdef CONFIG_X86
95 	&raid6_recov_avx512,
96 	&raid6_recov_avx2,
97 	&raid6_recov_ssse3,
98 #endif
99 #ifdef CONFIG_S390
100 	&raid6_recov_s390xc,
101 #endif
102 #if defined(CONFIG_KERNEL_MODE_NEON)
103 	&raid6_recov_neon,
104 #endif
105 #ifdef CONFIG_LOONGARCH
106 #ifdef CONFIG_CPU_HAS_LASX
107 	&raid6_recov_lasx,
108 #endif
109 #ifdef CONFIG_CPU_HAS_LSX
110 	&raid6_recov_lsx,
111 #endif
112 #endif
113 	&raid6_recov_intx1,
114 	NULL
115 };
116 
117 #ifdef __KERNEL__
118 #define RAID6_TIME_JIFFIES_LG2	4
119 #else
120 /* Need more time to be stable in userspace */
121 #define RAID6_TIME_JIFFIES_LG2	9
122 #define time_before(x, y) ((x) < (y))
123 #endif
124 
125 #define RAID6_TEST_DISKS	8
126 #define RAID6_TEST_DISKS_ORDER	3
127 
128 static inline const struct raid6_recov_calls *raid6_choose_recov(void)
129 {
130 	const struct raid6_recov_calls *const *algo;
131 	const struct raid6_recov_calls *best;
132 
133 	for (best = NULL, algo = raid6_recov_algos; *algo; algo++)
134 		if (!best || (*algo)->priority > best->priority)
135 			if (!(*algo)->valid || (*algo)->valid())
136 				best = *algo;
137 
138 	if (best) {
139 		raid6_2data_recov = best->data2;
140 		raid6_datap_recov = best->datap;
141 
142 		pr_info("raid6: using %s recovery algorithm\n", best->name);
143 	} else
144 		pr_err("raid6: Yikes! No recovery algorithm found!\n");
145 
146 	return best;
147 }
148 
149 static inline const struct raid6_calls *raid6_choose_gen(
150 	void *(*const dptrs)[RAID6_TEST_DISKS], const int disks)
151 {
152 	unsigned long perf, bestgenperf, j0, j1;
153 	int start = (disks>>1)-1, stop = disks-3;	/* work on the second half of the disks */
154 	const struct raid6_calls *const *algo;
155 	const struct raid6_calls *best;
156 
157 	for (bestgenperf = 0, best = NULL, algo = raid6_algos; *algo; algo++) {
158 		if (!best || (*algo)->priority >= best->priority) {
159 			if ((*algo)->valid && !(*algo)->valid())
160 				continue;
161 
162 			if (!IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK)) {
163 				best = *algo;
164 				break;
165 			}
166 
167 			perf = 0;
168 
169 			preempt_disable();
170 			j0 = jiffies;
171 			while ((j1 = jiffies) == j0)
172 				cpu_relax();
173 			while (time_before(jiffies,
174 					    j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
175 				(*algo)->gen_syndrome(disks, PAGE_SIZE, *dptrs);
176 				perf++;
177 			}
178 			preempt_enable();
179 
180 			if (perf > bestgenperf) {
181 				bestgenperf = perf;
182 				best = *algo;
183 			}
184 			pr_info("raid6: %-8s gen() %5ld MB/s\n", (*algo)->name,
185 				(perf * HZ * (disks-2)) >>
186 				(20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2));
187 		}
188 	}
189 
190 	if (!best) {
191 		pr_err("raid6: Yikes! No algorithm found!\n");
192 		goto out;
193 	}
194 
195 	raid6_call = *best;
196 
197 	if (!IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK)) {
198 		pr_info("raid6: skipped pq benchmark and selected %s\n",
199 			best->name);
200 		goto out;
201 	}
202 
203 	pr_info("raid6: using algorithm %s gen() %ld MB/s\n",
204 		best->name,
205 		(bestgenperf * HZ * (disks - 2)) >>
206 		(20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2));
207 
208 	if (best->xor_syndrome) {
209 		perf = 0;
210 
211 		preempt_disable();
212 		j0 = jiffies;
213 		while ((j1 = jiffies) == j0)
214 			cpu_relax();
215 		while (time_before(jiffies,
216 				   j1 + (1 << RAID6_TIME_JIFFIES_LG2))) {
217 			best->xor_syndrome(disks, start, stop,
218 					   PAGE_SIZE, *dptrs);
219 			perf++;
220 		}
221 		preempt_enable();
222 
223 		pr_info("raid6: .... xor() %ld MB/s, rmw enabled\n",
224 			(perf * HZ * (disks - 2)) >>
225 			(20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2 + 1));
226 	}
227 
228 out:
229 	return best;
230 }
231 
232 
233 /* Try to pick the best algorithm */
234 /* This code uses the gfmul table as convenient data set to abuse */
235 
236 int __init raid6_select_algo(void)
237 {
238 	const int disks = RAID6_TEST_DISKS;
239 
240 	const struct raid6_calls *gen_best;
241 	const struct raid6_recov_calls *rec_best;
242 	char *disk_ptr, *p;
243 	void *dptrs[RAID6_TEST_DISKS];
244 	int i, cycle;
245 
246 	/* prepare the buffer and fill it circularly with gfmul table */
247 	disk_ptr = (char *)__get_free_pages(GFP_KERNEL, RAID6_TEST_DISKS_ORDER);
248 	if (!disk_ptr) {
249 		pr_err("raid6: Yikes!  No memory available.\n");
250 		return -ENOMEM;
251 	}
252 
253 	p = disk_ptr;
254 	for (i = 0; i < disks; i++)
255 		dptrs[i] = p + PAGE_SIZE * i;
256 
257 	cycle = ((disks - 2) * PAGE_SIZE) / 65536;
258 	for (i = 0; i < cycle; i++) {
259 		memcpy(p, raid6_gfmul, 65536);
260 		p += 65536;
261 	}
262 
263 	if ((disks - 2) * PAGE_SIZE % 65536)
264 		memcpy(p, raid6_gfmul, (disks - 2) * PAGE_SIZE % 65536);
265 
266 	/* select raid gen_syndrome function */
267 	gen_best = raid6_choose_gen(&dptrs, disks);
268 
269 	/* select raid recover functions */
270 	rec_best = raid6_choose_recov();
271 
272 	free_pages((unsigned long)disk_ptr, RAID6_TEST_DISKS_ORDER);
273 
274 	return gen_best && rec_best ? 0 : -EINVAL;
275 }
276 
277 static void raid6_exit(void)
278 {
279 	do { } while (0);
280 }
281 
282 subsys_initcall(raid6_select_algo);
283 module_exit(raid6_exit);
284 MODULE_LICENSE("GPL");
285 MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");
286