]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/vdev_raidz_math.c
Add AltiVec RAID-Z
[mirror_zfs.git] / module / zfs / vdev_raidz_math.c
CommitLineData
ab9f4b0b
GN
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
23 */
24
25#include <sys/zfs_context.h>
26#include <sys/types.h>
27#include <sys/zio.h>
28#include <sys/debug.h>
29#include <sys/zfs_debug.h>
ab9f4b0b
GN
30#include <sys/vdev_raidz.h>
31#include <sys/vdev_raidz_impl.h>
006e9a40 32#include <sys/simd.h>
ab9f4b0b 33
c9187d86
GN
34extern boolean_t raidz_will_scalar_work(void);
35
36/* Opaque implementation with NULL methods to represent original methods */
37static const raidz_impl_ops_t vdev_raidz_original_impl = {
38 .name = "original",
39 .is_supported = raidz_will_scalar_work,
40};
41
42/* RAIDZ parity op that contain the fastest methods */
43static raidz_impl_ops_t vdev_raidz_fastest_impl = {
44 .name = "fastest"
45};
46
ab9f4b0b
GN
47/* All compiled in implementations */
48const raidz_impl_ops_t *raidz_all_maths[] = {
c9187d86 49 &vdev_raidz_original_impl,
ab9f4b0b 50 &vdev_raidz_scalar_impl,
ae25d222
GN
51#if defined(__x86_64) && defined(HAVE_SSE2) /* only x86_64 for now */
52 &vdev_raidz_sse2_impl,
53#endif
ab9f4b0b 54#if defined(__x86_64) && defined(HAVE_SSSE3) /* only x86_64 for now */
ae25d222 55 &vdev_raidz_ssse3_impl,
ab9f4b0b
GN
56#endif
57#if defined(__x86_64) && defined(HAVE_AVX2) /* only x86_64 for now */
62a65a65
RD
58 &vdev_raidz_avx2_impl,
59#endif
7f547f85 60#if defined(__x86_64) && defined(HAVE_AVX512F) /* only x86_64 for now */
65d71d42 61 &vdev_raidz_avx512f_impl,
7f547f85
RD
62#endif
63#if defined(__x86_64) && defined(HAVE_AVX512BW) /* only x86_64 for now */
01017962 64 &vdev_raidz_avx512bw_impl,
7f547f85 65#endif
62a65a65 66#if defined(__aarch64__)
88cc2352
RD
67 &vdev_raidz_aarch64_neon_impl,
68 &vdev_raidz_aarch64_neonx2_impl,
ab9f4b0b 69#endif
35b07497
RD
70#if defined(__powerpc__)
71 &vdev_raidz_powerpc_altivec_impl,
72#endif
ab9f4b0b
GN
73};
74
75/* Indicate that benchmark has been completed */
76static boolean_t raidz_math_initialized = B_FALSE;
77
78/* Select raidz implementation */
c9187d86
GN
79#define IMPL_FASTEST (UINT32_MAX)
80#define IMPL_CYCLE (UINT32_MAX - 1)
81#define IMPL_ORIGINAL (0)
82#define IMPL_SCALAR (1)
83
84#define RAIDZ_IMPL_READ(i) (*(volatile uint32_t *) &(i))
85
86static uint32_t zfs_vdev_raidz_impl = IMPL_SCALAR;
87static uint32_t user_sel_impl = IMPL_FASTEST;
ab9f4b0b
GN
88
89/* Hold all supported implementations */
c9187d86
GN
90static size_t raidz_supp_impl_cnt = 0;
91static raidz_impl_ops_t *raidz_supp_impl[ARRAY_SIZE(raidz_all_maths)];
ab9f4b0b 92
e5db3134 93#if defined(_KERNEL)
ab9f4b0b 94/*
26a08b5c
GN
95 * kstats values for supported implementations
96 * Values represent per disk throughput of 8 disk+parity raidz vdev [B/s]
ab9f4b0b
GN
97 */
98static raidz_impl_kstat_t raidz_impl_kstats[ARRAY_SIZE(raidz_all_maths) + 1];
99
100/* kstat for benchmarked implementations */
101static kstat_t *raidz_math_kstat = NULL;
e5db3134 102#endif
ab9f4b0b
GN
103
104/*
e5db3134
BB
105 * Returns the RAIDZ operations for raidz_map() parity calculations. When
106 * a SIMD implementation is not allowed in the current context, then fallback
107 * to the fastest generic implementation.
ab9f4b0b 108 */
e5db3134
BB
109const raidz_impl_ops_t *
110vdev_raidz_math_get_ops(void)
ab9f4b0b 111{
e5db3134
BB
112 if (!kfpu_allowed())
113 return (&vdev_raidz_scalar_impl);
114
c9187d86
GN
115 raidz_impl_ops_t *ops = NULL;
116 const uint32_t impl = RAIDZ_IMPL_READ(zfs_vdev_raidz_impl);
117
118 switch (impl) {
119 case IMPL_FASTEST:
120 ASSERT(raidz_math_initialized);
121 ops = &vdev_raidz_fastest_impl;
122 break;
c9187d86 123 case IMPL_CYCLE:
e5db3134 124 /* Cycle through all supported implementations */
c9187d86
GN
125 ASSERT(raidz_math_initialized);
126 ASSERT3U(raidz_supp_impl_cnt, >, 0);
ab9f4b0b 127 static size_t cycle_impl_idx = 0;
c9187d86
GN
128 size_t idx = (++cycle_impl_idx) % raidz_supp_impl_cnt;
129 ops = raidz_supp_impl[idx];
e5db3134 130 break;
c9187d86 131 case IMPL_ORIGINAL:
02730c33 132 ops = (raidz_impl_ops_t *)&vdev_raidz_original_impl;
c9187d86
GN
133 break;
134 case IMPL_SCALAR:
02730c33 135 ops = (raidz_impl_ops_t *)&vdev_raidz_scalar_impl;
c9187d86
GN
136 break;
137 default:
c9187d86
GN
138 ASSERT3U(impl, <, raidz_supp_impl_cnt);
139 ASSERT3U(raidz_supp_impl_cnt, >, 0);
d99a0153
CW
140 if (impl < ARRAY_SIZE(raidz_all_maths))
141 ops = raidz_supp_impl[impl];
c9187d86
GN
142 break;
143 }
ab9f4b0b 144
c9187d86
GN
145 ASSERT3P(ops, !=, NULL);
146
147 return (ops);
ab9f4b0b
GN
148}
149
150/*
151 * Select parity generation method for raidz_map
152 */
c9187d86 153int
ab9f4b0b
GN
154vdev_raidz_math_generate(raidz_map_t *rm)
155{
156 raidz_gen_f gen_parity = NULL;
157
158 switch (raidz_parity(rm)) {
159 case 1:
160 gen_parity = rm->rm_ops->gen[RAIDZ_GEN_P];
161 break;
162 case 2:
163 gen_parity = rm->rm_ops->gen[RAIDZ_GEN_PQ];
164 break;
165 case 3:
166 gen_parity = rm->rm_ops->gen[RAIDZ_GEN_PQR];
167 break;
168 default:
169 gen_parity = NULL;
170 cmn_err(CE_PANIC, "invalid RAID-Z configuration %d",
02730c33 171 raidz_parity(rm));
ab9f4b0b
GN
172 break;
173 }
174
c9187d86
GN
175 /* if method is NULL execute the original implementation */
176 if (gen_parity == NULL)
177 return (RAIDZ_ORIGINAL_IMPL);
ab9f4b0b
GN
178
179 gen_parity(rm);
c9187d86
GN
180
181 return (0);
ab9f4b0b
GN
182}
183
184static raidz_rec_f
c9187d86 185reconstruct_fun_p_sel(raidz_map_t *rm, const int *parity_valid,
4ea3f864 186 const int nbaddata)
ab9f4b0b
GN
187{
188 if (nbaddata == 1 && parity_valid[CODE_P]) {
189 return (rm->rm_ops->rec[RAIDZ_REC_P]);
190 }
191 return ((raidz_rec_f) NULL);
192}
193
194static raidz_rec_f
c9187d86 195reconstruct_fun_pq_sel(raidz_map_t *rm, const int *parity_valid,
4ea3f864 196 const int nbaddata)
ab9f4b0b
GN
197{
198 if (nbaddata == 1) {
199 if (parity_valid[CODE_P]) {
200 return (rm->rm_ops->rec[RAIDZ_REC_P]);
201 } else if (parity_valid[CODE_Q]) {
202 return (rm->rm_ops->rec[RAIDZ_REC_Q]);
203 }
204 } else if (nbaddata == 2 &&
02730c33 205 parity_valid[CODE_P] && parity_valid[CODE_Q]) {
ab9f4b0b
GN
206 return (rm->rm_ops->rec[RAIDZ_REC_PQ]);
207 }
208 return ((raidz_rec_f) NULL);
209}
210
211static raidz_rec_f
c9187d86 212reconstruct_fun_pqr_sel(raidz_map_t *rm, const int *parity_valid,
4ea3f864 213 const int nbaddata)
ab9f4b0b
GN
214{
215 if (nbaddata == 1) {
216 if (parity_valid[CODE_P]) {
217 return (rm->rm_ops->rec[RAIDZ_REC_P]);
218 } else if (parity_valid[CODE_Q]) {
219 return (rm->rm_ops->rec[RAIDZ_REC_Q]);
220 } else if (parity_valid[CODE_R]) {
221 return (rm->rm_ops->rec[RAIDZ_REC_R]);
222 }
223 } else if (nbaddata == 2) {
224 if (parity_valid[CODE_P] && parity_valid[CODE_Q]) {
225 return (rm->rm_ops->rec[RAIDZ_REC_PQ]);
226 } else if (parity_valid[CODE_P] && parity_valid[CODE_R]) {
227 return (rm->rm_ops->rec[RAIDZ_REC_PR]);
228 } else if (parity_valid[CODE_Q] && parity_valid[CODE_R]) {
229 return (rm->rm_ops->rec[RAIDZ_REC_QR]);
230 }
231 } else if (nbaddata == 3 &&
02730c33
BB
232 parity_valid[CODE_P] && parity_valid[CODE_Q] &&
233 parity_valid[CODE_R]) {
ab9f4b0b
GN
234 return (rm->rm_ops->rec[RAIDZ_REC_PQR]);
235 }
236 return ((raidz_rec_f) NULL);
237}
238
239/*
240 * Select data reconstruction method for raidz_map
241 * @parity_valid - Parity validity flag
242 * @dt - Failed data index array
243 * @nbaddata - Number of failed data columns
244 */
245int
246vdev_raidz_math_reconstruct(raidz_map_t *rm, const int *parity_valid,
4ea3f864 247 const int *dt, const int nbaddata)
ab9f4b0b 248{
cbf484f8 249 raidz_rec_f rec_fn = NULL;
ab9f4b0b
GN
250
251 switch (raidz_parity(rm)) {
c9187d86 252 case PARITY_P:
cbf484f8 253 rec_fn = reconstruct_fun_p_sel(rm, parity_valid, nbaddata);
c9187d86
GN
254 break;
255 case PARITY_PQ:
cbf484f8 256 rec_fn = reconstruct_fun_pq_sel(rm, parity_valid, nbaddata);
c9187d86
GN
257 break;
258 case PARITY_PQR:
cbf484f8 259 rec_fn = reconstruct_fun_pqr_sel(rm, parity_valid, nbaddata);
c9187d86
GN
260 break;
261 default:
262 cmn_err(CE_PANIC, "invalid RAID-Z configuration %d",
263 raidz_parity(rm));
264 break;
ab9f4b0b
GN
265 }
266
cbf484f8 267 if (rec_fn == NULL)
c9187d86
GN
268 return (RAIDZ_ORIGINAL_IMPL);
269 else
cbf484f8 270 return (rec_fn(rm, dt));
ab9f4b0b
GN
271}
272
273const char *raidz_gen_name[] = {
274 "gen_p", "gen_pq", "gen_pqr"
275};
276const char *raidz_rec_name[] = {
277 "rec_p", "rec_q", "rec_r",
278 "rec_pq", "rec_pr", "rec_qr", "rec_pqr"
279};
280
e5db3134
BB
281#if defined(_KERNEL)
282
26a08b5c
GN
283#define RAIDZ_KSTAT_LINE_LEN (17 + 10*12 + 1)
284
285static int
286raidz_math_kstat_headers(char *buf, size_t size)
ab9f4b0b
GN
287{
288 int i;
26a08b5c
GN
289 ssize_t off;
290
291 ASSERT3U(size, >=, RAIDZ_KSTAT_LINE_LEN);
292
293 off = snprintf(buf, size, "%-17s", "implementation");
294
295 for (i = 0; i < ARRAY_SIZE(raidz_gen_name); i++)
62a65a65 296 off += snprintf(buf + off, size - off, "%-16s",
26a08b5c
GN
297 raidz_gen_name[i]);
298
299 for (i = 0; i < ARRAY_SIZE(raidz_rec_name); i++)
62a65a65 300 off += snprintf(buf + off, size - off, "%-16s",
26a08b5c
GN
301 raidz_rec_name[i]);
302
303 (void) snprintf(buf + off, size - off, "\n");
304
305 return (0);
306}
307
308static int
309raidz_math_kstat_data(char *buf, size_t size, void *data)
310{
02730c33
BB
311 raidz_impl_kstat_t *fstat = &raidz_impl_kstats[raidz_supp_impl_cnt];
312 raidz_impl_kstat_t *cstat = (raidz_impl_kstat_t *)data;
26a08b5c
GN
313 ssize_t off = 0;
314 int i;
315
316 ASSERT3U(size, >=, RAIDZ_KSTAT_LINE_LEN);
ab9f4b0b 317
26a08b5c
GN
318 if (cstat == fstat) {
319 off += snprintf(buf + off, size - off, "%-17s", "fastest");
ab9f4b0b 320
26a08b5c
GN
321 for (i = 0; i < ARRAY_SIZE(raidz_gen_name); i++) {
322 int id = fstat->gen[i];
62a65a65 323 off += snprintf(buf + off, size - off, "%-16s",
26a08b5c
GN
324 raidz_supp_impl[id]->name);
325 }
326 for (i = 0; i < ARRAY_SIZE(raidz_rec_name); i++) {
327 int id = fstat->rec[i];
62a65a65 328 off += snprintf(buf + off, size - off, "%-16s",
26a08b5c
GN
329 raidz_supp_impl[id]->name);
330 }
331 } else {
332 ptrdiff_t id = cstat - raidz_impl_kstats;
333
334 off += snprintf(buf + off, size - off, "%-17s",
335 raidz_supp_impl[id]->name);
336
337 for (i = 0; i < ARRAY_SIZE(raidz_gen_name); i++)
62a65a65 338 off += snprintf(buf + off, size - off, "%-16llu",
02730c33 339 (u_longlong_t)cstat->gen[i]);
26a08b5c
GN
340
341 for (i = 0; i < ARRAY_SIZE(raidz_rec_name); i++)
62a65a65 342 off += snprintf(buf + off, size - off, "%-16llu",
02730c33 343 (u_longlong_t)cstat->rec[i]);
ab9f4b0b 344 }
26a08b5c
GN
345
346 (void) snprintf(buf + off, size - off, "\n");
347
348 return (0);
349}
350
351static void *
352raidz_math_kstat_addr(kstat_t *ksp, loff_t n)
353{
354 if (n <= raidz_supp_impl_cnt)
355 ksp->ks_private = (void *) (raidz_impl_kstats + n);
356 else
357 ksp->ks_private = NULL;
358
359 return (ksp->ks_private);
ab9f4b0b
GN
360}
361
362#define BENCH_D_COLS (8ULL)
363#define BENCH_COLS (BENCH_D_COLS + PARITY_PQR)
590c9a09 364#define BENCH_ZIO_SIZE (1ULL << SPA_OLD_MAXBLOCKSHIFT) /* 128 kiB */
ab9f4b0b
GN
365#define BENCH_NS MSEC2NSEC(25) /* 25ms */
366
367typedef void (*benchmark_fn)(raidz_map_t *rm, const int fn);
368
369static void
370benchmark_gen_impl(raidz_map_t *rm, const int fn)
371{
372 (void) fn;
373 vdev_raidz_generate_parity(rm);
374}
375
376static void
377benchmark_rec_impl(raidz_map_t *rm, const int fn)
378{
379 static const int rec_tgt[7][3] = {
380 {1, 2, 3}, /* rec_p: bad QR & D[0] */
381 {0, 2, 3}, /* rec_q: bad PR & D[0] */
382 {0, 1, 3}, /* rec_r: bad PQ & D[0] */
383 {2, 3, 4}, /* rec_pq: bad R & D[0][1] */
384 {1, 3, 4}, /* rec_pr: bad Q & D[0][1] */
385 {0, 3, 4}, /* rec_qr: bad P & D[0][1] */
386 {3, 4, 5} /* rec_pqr: bad & D[0][1][2] */
387 };
388
389 vdev_raidz_reconstruct(rm, rec_tgt[fn], 3);
390}
391
392/*
393 * Benchmarking of all supported implementations (raidz_supp_impl_cnt)
394 * is performed by setting the rm_ops pointer and calling the top level
395 * generate/reconstruct methods of bench_rm.
396 */
397static void
398benchmark_raidz_impl(raidz_map_t *bench_rm, const int fn, benchmark_fn bench_fn)
399{
400 uint64_t run_cnt, speed, best_speed = 0;
401 hrtime_t t_start, t_diff;
402 raidz_impl_ops_t *curr_impl;
02730c33 403 raidz_impl_kstat_t *fstat = &raidz_impl_kstats[raidz_supp_impl_cnt];
ab9f4b0b
GN
404 int impl, i;
405
c9187d86 406 for (impl = 0; impl < raidz_supp_impl_cnt; impl++) {
ab9f4b0b
GN
407 /* set an implementation to benchmark */
408 curr_impl = raidz_supp_impl[impl];
409 bench_rm->rm_ops = curr_impl;
410
411 run_cnt = 0;
412 t_start = gethrtime();
413
414 do {
415 for (i = 0; i < 25; i++, run_cnt++)
416 bench_fn(bench_rm, fn);
417
418 t_diff = gethrtime() - t_start;
419 } while (t_diff < BENCH_NS);
420
421 speed = run_cnt * BENCH_ZIO_SIZE * NANOSEC;
422 speed /= (t_diff * BENCH_COLS);
423
424 if (bench_fn == benchmark_gen_impl)
26a08b5c 425 raidz_impl_kstats[impl].gen[fn] = speed;
ab9f4b0b 426 else
26a08b5c 427 raidz_impl_kstats[impl].rec[fn] = speed;
ab9f4b0b 428
c9187d86
GN
429 /* Update fastest implementation method */
430 if (speed > best_speed) {
ab9f4b0b
GN
431 best_speed = speed;
432
c9187d86 433 if (bench_fn == benchmark_gen_impl) {
26a08b5c 434 fstat->gen[fn] = impl;
ab9f4b0b
GN
435 vdev_raidz_fastest_impl.gen[fn] =
436 curr_impl->gen[fn];
c9187d86 437 } else {
26a08b5c 438 fstat->rec[fn] = impl;
ab9f4b0b
GN
439 vdev_raidz_fastest_impl.rec[fn] =
440 curr_impl->rec[fn];
c9187d86 441 }
ab9f4b0b
GN
442 }
443 }
444}
e5db3134 445#endif
ab9f4b0b 446
e5db3134
BB
447/*
448 * Initialize and benchmark all supported implementations.
449 */
450static void
10fa2545 451benchmark_raidz(void)
ab9f4b0b
GN
452{
453 raidz_impl_ops_t *curr_impl;
e5db3134 454 int i, c;
ab9f4b0b 455
e5db3134 456 /* Move supported impl into raidz_supp_impl */
ab9f4b0b 457 for (i = 0, c = 0; i < ARRAY_SIZE(raidz_all_maths); i++) {
02730c33 458 curr_impl = (raidz_impl_ops_t *)raidz_all_maths[i];
ab9f4b0b 459
ab9f4b0b
GN
460 if (curr_impl->init)
461 curr_impl->init();
462
26a08b5c 463 if (curr_impl->is_supported())
02730c33 464 raidz_supp_impl[c++] = (raidz_impl_ops_t *)curr_impl;
ab9f4b0b 465 }
c9187d86 466 membar_producer(); /* complete raidz_supp_impl[] init */
ab9f4b0b 467 raidz_supp_impl_cnt = c; /* number of supported impl */
ab9f4b0b 468
e5db3134
BB
469#if defined(_KERNEL)
470 zio_t *bench_zio = NULL;
471 raidz_map_t *bench_rm = NULL;
472 uint64_t bench_parity;
ab9f4b0b 473
10269e02 474 /* Fake a zio and run the benchmark on a warmed up buffer */
ab9f4b0b
GN
475 bench_zio = kmem_zalloc(sizeof (zio_t), KM_SLEEP);
476 bench_zio->io_offset = 0;
477 bench_zio->io_size = BENCH_ZIO_SIZE; /* only data columns */
a6255b7f
DQ
478 bench_zio->io_abd = abd_alloc_linear(BENCH_ZIO_SIZE, B_TRUE);
479 memset(abd_to_buf(bench_zio->io_abd), 0xAA, BENCH_ZIO_SIZE);
ab9f4b0b
GN
480
481 /* Benchmark parity generation methods */
e5db3134 482 for (int fn = 0; fn < RAIDZ_GEN_NUM; fn++) {
ab9f4b0b
GN
483 bench_parity = fn + 1;
484 /* New raidz_map is needed for each generate_p/q/r */
c9187d86 485 bench_rm = vdev_raidz_map_alloc(bench_zio, SPA_MINBLOCKSHIFT,
ab9f4b0b
GN
486 BENCH_D_COLS + bench_parity, bench_parity);
487
488 benchmark_raidz_impl(bench_rm, fn, benchmark_gen_impl);
489
490 vdev_raidz_map_free(bench_rm);
491 }
492
493 /* Benchmark data reconstruction methods */
c9187d86
GN
494 bench_rm = vdev_raidz_map_alloc(bench_zio, SPA_MINBLOCKSHIFT,
495 BENCH_COLS, PARITY_PQR);
ab9f4b0b 496
e5db3134 497 for (int fn = 0; fn < RAIDZ_REC_NUM; fn++)
ab9f4b0b
GN
498 benchmark_raidz_impl(bench_rm, fn, benchmark_rec_impl);
499
500 vdev_raidz_map_free(bench_rm);
501
502 /* cleanup the bench zio */
a6255b7f 503 abd_free(bench_zio->io_abd);
ab9f4b0b 504 kmem_free(bench_zio, sizeof (zio_t));
e5db3134
BB
505#else
506 /*
507 * Skip the benchmark in user space to avoid impacting libzpool
508 * consumers (zdb, zhack, zinject, ztest). The last implementation
509 * is assumed to be the fastest and used by default.
510 */
511 memcpy(&vdev_raidz_fastest_impl,
512 raidz_supp_impl[raidz_supp_impl_cnt - 1],
513 sizeof (vdev_raidz_fastest_impl));
514 strcpy(vdev_raidz_fastest_impl.name, "fastest");
515#endif /* _KERNEL */
516}
517
518void
519vdev_raidz_math_init(void)
520{
10fa2545
BB
521 /* Determine the fastest available implementation. */
522 benchmark_raidz();
ab9f4b0b 523
10fa2545 524#if defined(_KERNEL)
e5db3134 525 /* Install kstats for all implementations */
26a08b5c 526 raidz_math_kstat = kstat_create("zfs", 0, "vdev_raidz_bench", "misc",
02730c33 527 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
ab9f4b0b 528 if (raidz_math_kstat != NULL) {
26a08b5c
GN
529 raidz_math_kstat->ks_data = NULL;
530 raidz_math_kstat->ks_ndata = UINT32_MAX;
531 kstat_set_raw_ops(raidz_math_kstat,
532 raidz_math_kstat_headers,
533 raidz_math_kstat_data,
534 raidz_math_kstat_addr);
ab9f4b0b
GN
535 kstat_install(raidz_math_kstat);
536 }
e5db3134 537#endif
ab9f4b0b
GN
538
539 /* Finish initialization */
c9187d86 540 atomic_swap_32(&zfs_vdev_raidz_impl, user_sel_impl);
ab9f4b0b 541 raidz_math_initialized = B_TRUE;
ab9f4b0b
GN
542}
543
544void
545vdev_raidz_math_fini(void)
546{
547 raidz_impl_ops_t const *curr_impl;
ab9f4b0b 548
e5db3134 549#if defined(_KERNEL)
ab9f4b0b
GN
550 if (raidz_math_kstat != NULL) {
551 kstat_delete(raidz_math_kstat);
552 raidz_math_kstat = NULL;
553 }
e5db3134 554#endif
ab9f4b0b 555
e5db3134 556 for (int i = 0; i < ARRAY_SIZE(raidz_all_maths); i++) {
ab9f4b0b 557 curr_impl = raidz_all_maths[i];
ab9f4b0b
GN
558 if (curr_impl->fini)
559 curr_impl->fini();
560 }
561}
562
c9187d86 563static const struct {
02730c33 564 char *name;
c9187d86 565 uint32_t sel;
ab9f4b0b 566} math_impl_opts[] = {
c9187d86 567 { "cycle", IMPL_CYCLE },
c9187d86
GN
568 { "fastest", IMPL_FASTEST },
569 { "original", IMPL_ORIGINAL },
570 { "scalar", IMPL_SCALAR }
ab9f4b0b
GN
571};
572
573/*
574 * Function sets desired raidz implementation.
c9187d86
GN
575 *
576 * If we are called before init(), user preference will be saved in
577 * user_sel_impl, and applied in later init() call. This occurs when module
578 * parameter is specified on module load. Otherwise, directly update
579 * zfs_vdev_raidz_impl.
ab9f4b0b
GN
580 *
581 * @val Name of raidz implementation to use
582 * @param Unused.
583 */
9cc1844a
GN
584int
585vdev_raidz_impl_set(const char *val)
ab9f4b0b 586{
c9187d86
GN
587 int err = -EINVAL;
588 char req_name[RAIDZ_IMPL_NAME_MAX];
589 uint32_t impl = RAIDZ_IMPL_READ(user_sel_impl);
ab9f4b0b
GN
590 size_t i;
591
c9187d86
GN
592 /* sanitize input */
593 i = strnlen(val, RAIDZ_IMPL_NAME_MAX);
594 if (i == 0 || i == RAIDZ_IMPL_NAME_MAX)
595 return (err);
596
597 strlcpy(req_name, val, RAIDZ_IMPL_NAME_MAX);
598 while (i > 0 && !!isspace(req_name[i-1]))
599 i--;
600 req_name[i] = '\0';
601
ab9f4b0b
GN
602 /* Check mandatory options */
603 for (i = 0; i < ARRAY_SIZE(math_impl_opts); i++) {
c9187d86
GN
604 if (strcmp(req_name, math_impl_opts[i].name) == 0) {
605 impl = math_impl_opts[i].sel;
606 err = 0;
607 break;
ab9f4b0b
GN
608 }
609 }
610
c9187d86
GN
611 /* check all supported impl if init() was already called */
612 if (err != 0 && raidz_math_initialized) {
613 /* check all supported implementations */
614 for (i = 0; i < raidz_supp_impl_cnt; i++) {
615 if (strcmp(req_name, raidz_supp_impl[i]->name) == 0) {
616 impl = i;
617 err = 0;
618 break;
619 }
ab9f4b0b
GN
620 }
621 }
622
c9187d86
GN
623 if (err == 0) {
624 if (raidz_math_initialized)
625 atomic_swap_32(&zfs_vdev_raidz_impl, impl);
626 else
627 atomic_swap_32(&user_sel_impl, impl);
628 }
629
630 return (err);
ab9f4b0b
GN
631}
632
b3673342 633#if defined(_KERNEL) && defined(__linux__)
ab9f4b0b 634
9cc1844a
GN
635static int
636zfs_vdev_raidz_impl_set(const char *val, zfs_kernel_param_t *kp)
637{
638 return (vdev_raidz_impl_set(val));
ab9f4b0b
GN
639}
640
ab9f4b0b 641static int
9cc1844a 642zfs_vdev_raidz_impl_get(char *buffer, zfs_kernel_param_t *kp)
ab9f4b0b
GN
643{
644 int i, cnt = 0;
645 char *fmt;
c9187d86 646 const uint32_t impl = RAIDZ_IMPL_READ(zfs_vdev_raidz_impl);
ab9f4b0b
GN
647
648 ASSERT(raidz_math_initialized);
649
ab9f4b0b 650 /* list mandatory options */
c9187d86
GN
651 for (i = 0; i < ARRAY_SIZE(math_impl_opts) - 2; i++) {
652 fmt = (impl == math_impl_opts[i].sel) ? "[%s] " : "%s ";
ab9f4b0b
GN
653 cnt += sprintf(buffer + cnt, fmt, math_impl_opts[i].name);
654 }
655
656 /* list all supported implementations */
657 for (i = 0; i < raidz_supp_impl_cnt; i++) {
c9187d86 658 fmt = (i == impl) ? "[%s] " : "%s ";
ab9f4b0b
GN
659 cnt += sprintf(buffer + cnt, fmt, raidz_supp_impl[i]->name);
660 }
661
ab9f4b0b
GN
662 return (cnt);
663}
664
665module_param_call(zfs_vdev_raidz_impl, zfs_vdev_raidz_impl_set,
4ea3f864 666 zfs_vdev_raidz_impl_get, NULL, 0644);
ab9f4b0b
GN
667MODULE_PARM_DESC(zfs_vdev_raidz_impl, "Select raidz implementation.");
668#endif