]> git.proxmox.com Git - mirror_zfs.git/blame - module/zcommon/zfs_fletcher.c
Add limits to spa_slop_shift tunable
[mirror_zfs.git] / module / zcommon / zfs_fletcher.c
CommitLineData
34dc7c2f
BB
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/*
9babb374 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
34dc7c2f 23 * Use is subject to license terms.
fc897b24 24 * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
34dc7c2f 25 */
3c67d83a
TH
26/*
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 */
34dc7c2f 29
a6255b7f
DQ
30/*
31 * Copyright (c) 2016 by Delphix. All rights reserved.
32 */
33
9babb374
BB
34/*
35 * Fletcher Checksums
36 * ------------------
37 *
38 * ZFS's 2nd and 4th order Fletcher checksums are defined by the following
39 * recurrence relations:
40 *
41 * a = a + f
42 * i i-1 i-1
43 *
44 * b = b + a
45 * i i-1 i
46 *
47 * c = c + b (fletcher-4 only)
48 * i i-1 i
49 *
50 * d = d + c (fletcher-4 only)
51 * i i-1 i
52 *
53 * Where
54 * a_0 = b_0 = c_0 = d_0 = 0
55 * and
56 * f_0 .. f_(n-1) are the input data.
57 *
58 * Using standard techniques, these translate into the following series:
59 *
60 * __n_ __n_
61 * \ | \ |
62 * a = > f b = > i * f
63 * n /___| n - i n /___| n - i
64 * i = 1 i = 1
65 *
66 *
67 * __n_ __n_
68 * \ | i*(i+1) \ | i*(i+1)*(i+2)
69 * c = > ------- f d = > ------------- f
70 * n /___| 2 n - i n /___| 6 n - i
71 * i = 1 i = 1
72 *
73 * For fletcher-2, the f_is are 64-bit, and [ab]_i are 64-bit accumulators.
74 * Since the additions are done mod (2^64), errors in the high bits may not
75 * be noticed. For this reason, fletcher-2 is deprecated.
76 *
77 * For fletcher-4, the f_is are 32-bit, and [abcd]_i are 64-bit accumulators.
78 * A conservative estimate of how big the buffer can get before we overflow
79 * can be estimated using f_i = 0xffffffff for all i:
80 *
81 * % bc
82 * f=2^32-1;d=0; for (i = 1; d<2^64; i++) { d += f*i*(i+1)*(i+2)/6 }; (i-1)*4
83 * 2264
84 * quit
85 * %
86 *
87 * So blocks of up to 2k will not overflow. Our largest block size is
88 * 128k, which has 32k 4-byte words, so we can compute the largest possible
89 * accumulators, then divide by 2^64 to figure the max amount of overflow:
90 *
91 * % bc
92 * a=b=c=d=0; f=2^32-1; for (i=1; i<=32*1024; i++) { a+=f; b+=a; c+=b; d+=c }
93 * a/2^64;b/2^64;c/2^64;d/2^64
94 * 0
95 * 0
96 * 1365
97 * 11186858
98 * quit
99 * %
100 *
101 * So a and b cannot overflow. To make sure each bit of input has some
102 * effect on the contents of c and d, we can look at what the factors of
103 * the coefficients in the equations for c_n and d_n are. The number of 2s
104 * in the factors determines the lowest set bit in the multiplier. Running
105 * through the cases for n*(n+1)/2 reveals that the highest power of 2 is
106 * 2^14, and for n*(n+1)*(n+2)/6 it is 2^15. So while some data may overflow
107 * the 64-bit accumulators, every bit of every f_i effects every accumulator,
108 * even for 128k blocks.
109 *
110 * If we wanted to make a stronger version of fletcher4 (fletcher4c?),
111 * we could do our calculations mod (2^32 - 1) by adding in the carries
112 * periodically, and store the number of carries in the top 32-bits.
113 *
114 * --------------------
115 * Checksum Performance
116 * --------------------
117 *
118 * There are two interesting components to checksum performance: cached and
119 * uncached performance. With cached data, fletcher-2 is about four times
120 * faster than fletcher-4. With uncached data, the performance difference is
121 * negligible, since the cost of a cache fill dominates the processing time.
122 * Even though fletcher-4 is slower than fletcher-2, it is still a pretty
123 * efficient pass over the data.
124 *
125 * In normal operation, the data which is being checksummed is in a buffer
126 * which has been filled either by:
127 *
128 * 1. a compression step, which will be mostly cached, or
129 * 2. a bcopy() or copyin(), which will be uncached (because the
130 * copy is cache-bypassing).
131 *
132 * For both cached and uncached data, both fletcher checksums are much faster
133 * than sha-256, and slower than 'off', which doesn't touch the data at all.
134 */
34dc7c2f
BB
135
136#include <sys/types.h>
137#include <sys/sysmacros.h>
138#include <sys/byteorder.h>
139#include <sys/spa.h>
fc897b24 140#include <sys/zio_checksum.h>
1eeb4562
JX
141#include <sys/zfs_context.h>
142#include <zfs_fletcher.h>
143
2fe36b0b 144#define FLETCHER_MIN_SIMD_SIZE 64
fc897b24 145
5bf703b8
GN
146static void fletcher_4_scalar_init(fletcher_4_ctx_t *ctx);
147static void fletcher_4_scalar_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp);
148static void fletcher_4_scalar_native(fletcher_4_ctx_t *ctx,
149 const void *buf, uint64_t size);
150static void fletcher_4_scalar_byteswap(fletcher_4_ctx_t *ctx,
151 const void *buf, uint64_t size);
1eeb4562
JX
152static boolean_t fletcher_4_scalar_valid(void);
153
154static const fletcher_4_ops_t fletcher_4_scalar_ops = {
fc897b24 155 .init_native = fletcher_4_scalar_init,
5bf703b8 156 .fini_native = fletcher_4_scalar_fini,
fc897b24
GN
157 .compute_native = fletcher_4_scalar_native,
158 .init_byteswap = fletcher_4_scalar_init,
5bf703b8 159 .fini_byteswap = fletcher_4_scalar_fini,
1eeb4562
JX
160 .compute_byteswap = fletcher_4_scalar_byteswap,
161 .valid = fletcher_4_scalar_valid,
162 .name = "scalar"
163};
164
fc897b24
GN
165static fletcher_4_ops_t fletcher_4_fastest_impl = {
166 .name = "fastest",
167 .valid = fletcher_4_scalar_valid
168};
169
170static const fletcher_4_ops_t *fletcher_4_impls[] = {
1eeb4562 171 &fletcher_4_scalar_ops,
7f319493
RD
172 &fletcher_4_superscalar_ops,
173 &fletcher_4_superscalar4_ops,
35a76a03
TS
174#if defined(HAVE_SSE2)
175 &fletcher_4_sse2_ops,
176#endif
177#if defined(HAVE_SSE2) && defined(HAVE_SSSE3)
178 &fletcher_4_ssse3_ops,
179#endif
1eeb4562
JX
180#if defined(HAVE_AVX) && defined(HAVE_AVX2)
181 &fletcher_4_avx2_ops,
182#endif
70b258fc
GN
183#if defined(__x86_64) && defined(HAVE_AVX512F)
184 &fletcher_4_avx512f_ops,
185#endif
24cdeaf1
RD
186#if defined(__aarch64__)
187 &fletcher_4_aarch64_neon_ops,
188#endif
1eeb4562
JX
189};
190
fc897b24
GN
191/* Hold all supported implementations */
192static uint32_t fletcher_4_supp_impls_cnt = 0;
193static fletcher_4_ops_t *fletcher_4_supp_impls[ARRAY_SIZE(fletcher_4_impls)];
194
195/* Select fletcher4 implementation */
196#define IMPL_FASTEST (UINT32_MAX)
197#define IMPL_CYCLE (UINT32_MAX - 1)
198#define IMPL_SCALAR (0)
199
200static uint32_t fletcher_4_impl_chosen = IMPL_FASTEST;
201
202#define IMPL_READ(i) (*(volatile uint32_t *) &(i))
1eeb4562
JX
203
204static struct fletcher_4_impl_selector {
fc897b24
GN
205 const char *fis_name;
206 uint32_t fis_sel;
1eeb4562 207} fletcher_4_impl_selectors[] = {
1eeb4562 208#if !defined(_KERNEL)
fc897b24 209 { "cycle", IMPL_CYCLE },
1eeb4562 210#endif
fc897b24
GN
211 { "fastest", IMPL_FASTEST },
212 { "scalar", IMPL_SCALAR }
1eeb4562
JX
213};
214
46364cb2 215#if defined(_KERNEL)
1eeb4562 216static kstat_t *fletcher_4_kstat;
46364cb2 217#endif
1eeb4562 218
fc897b24
GN
219static struct fletcher_4_kstat {
220 uint64_t native;
221 uint64_t byteswap;
222} fletcher_4_stat_data[ARRAY_SIZE(fletcher_4_impls) + 1];
223
224/* Indicate that benchmark has been completed */
225static boolean_t fletcher_4_initialized = B_FALSE;
34dc7c2f 226
3c67d83a 227/*ARGSUSED*/
34dc7c2f 228void
a6255b7f
DQ
229fletcher_init(zio_cksum_t *zcp)
230{
231 ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
232}
233
234int
235fletcher_2_incremental_native(void *buf, size_t size, void *data)
34dc7c2f 236{
a6255b7f
DQ
237 zio_cksum_t *zcp = data;
238
34dc7c2f
BB
239 const uint64_t *ip = buf;
240 const uint64_t *ipend = ip + (size / sizeof (uint64_t));
241 uint64_t a0, b0, a1, b1;
242
a6255b7f
DQ
243 a0 = zcp->zc_word[0];
244 a1 = zcp->zc_word[1];
245 b0 = zcp->zc_word[2];
246 b1 = zcp->zc_word[3];
247
248 for (; ip < ipend; ip += 2) {
34dc7c2f
BB
249 a0 += ip[0];
250 a1 += ip[1];
251 b0 += a0;
252 b1 += a1;
253 }
254
255 ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
a6255b7f 256 return (0);
34dc7c2f
BB
257}
258
3c67d83a 259/*ARGSUSED*/
34dc7c2f 260void
a6255b7f 261fletcher_2_native(const void *buf, uint64_t size,
3c67d83a 262 const void *ctx_template, zio_cksum_t *zcp)
34dc7c2f 263{
a6255b7f
DQ
264 fletcher_init(zcp);
265 (void) fletcher_2_incremental_native((void *) buf, size, zcp);
266}
267
268int
269fletcher_2_incremental_byteswap(void *buf, size_t size, void *data)
270{
271 zio_cksum_t *zcp = data;
272
34dc7c2f
BB
273 const uint64_t *ip = buf;
274 const uint64_t *ipend = ip + (size / sizeof (uint64_t));
275 uint64_t a0, b0, a1, b1;
276
a6255b7f
DQ
277 a0 = zcp->zc_word[0];
278 a1 = zcp->zc_word[1];
279 b0 = zcp->zc_word[2];
280 b1 = zcp->zc_word[3];
281
282 for (; ip < ipend; ip += 2) {
34dc7c2f
BB
283 a0 += BSWAP_64(ip[0]);
284 a1 += BSWAP_64(ip[1]);
285 b0 += a0;
286 b1 += a1;
287 }
288
289 ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
a6255b7f
DQ
290 return (0);
291}
292
293/*ARGSUSED*/
294void
295fletcher_2_byteswap(const void *buf, uint64_t size,
296 const void *ctx_template, zio_cksum_t *zcp)
297{
298 fletcher_init(zcp);
299 (void) fletcher_2_incremental_byteswap((void *) buf, size, zcp);
34dc7c2f
BB
300}
301
fc897b24 302static void
5bf703b8 303fletcher_4_scalar_init(fletcher_4_ctx_t *ctx)
34dc7c2f 304{
5bf703b8
GN
305 ZIO_SET_CHECKSUM(&ctx->scalar, 0, 0, 0, 0);
306}
307
308static void
309fletcher_4_scalar_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp)
310{
311 memcpy(zcp, &ctx->scalar, sizeof (zio_cksum_t));
34dc7c2f
BB
312}
313
1eeb4562 314static void
5bf703b8
GN
315fletcher_4_scalar_native(fletcher_4_ctx_t *ctx, const void *buf,
316 uint64_t size)
34dc7c2f
BB
317{
318 const uint32_t *ip = buf;
319 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
320 uint64_t a, b, c, d;
321
5bf703b8
GN
322 a = ctx->scalar.zc_word[0];
323 b = ctx->scalar.zc_word[1];
324 c = ctx->scalar.zc_word[2];
325 d = ctx->scalar.zc_word[3];
1eeb4562
JX
326
327 for (; ip < ipend; ip++) {
328 a += ip[0];
34dc7c2f
BB
329 b += a;
330 c += b;
331 d += c;
332 }
333
5bf703b8 334 ZIO_SET_CHECKSUM(&ctx->scalar, a, b, c, d);
34dc7c2f
BB
335}
336
1eeb4562 337static void
5bf703b8
GN
338fletcher_4_scalar_byteswap(fletcher_4_ctx_t *ctx, const void *buf,
339 uint64_t size)
34dc7c2f
BB
340{
341 const uint32_t *ip = buf;
342 const uint32_t *ipend = ip + (size / sizeof (uint32_t));
343 uint64_t a, b, c, d;
344
5bf703b8
GN
345 a = ctx->scalar.zc_word[0];
346 b = ctx->scalar.zc_word[1];
347 c = ctx->scalar.zc_word[2];
348 d = ctx->scalar.zc_word[3];
34dc7c2f
BB
349
350 for (; ip < ipend; ip++) {
1eeb4562 351 a += BSWAP_32(ip[0]);
34dc7c2f
BB
352 b += a;
353 c += b;
354 d += c;
355 }
356
5bf703b8 357 ZIO_SET_CHECKSUM(&ctx->scalar, a, b, c, d);
34dc7c2f
BB
358}
359
1eeb4562
JX
360static boolean_t
361fletcher_4_scalar_valid(void)
362{
363 return (B_TRUE);
364}
365
366int
367fletcher_4_impl_set(const char *val)
368{
fc897b24
GN
369 int err = -EINVAL;
370 uint32_t impl = IMPL_READ(fletcher_4_impl_chosen);
371 size_t i, val_len;
1eeb4562
JX
372
373 val_len = strlen(val);
374 while ((val_len > 0) && !!isspace(val[val_len-1])) /* trim '\n' */
375 val_len--;
376
fc897b24 377 /* check mandatory implementations */
1eeb4562
JX
378 for (i = 0; i < ARRAY_SIZE(fletcher_4_impl_selectors); i++) {
379 const char *name = fletcher_4_impl_selectors[i].fis_name;
380
381 if (val_len == strlen(name) &&
382 strncmp(val, name, val_len) == 0) {
fc897b24
GN
383 impl = fletcher_4_impl_selectors[i].fis_sel;
384 err = 0;
1eeb4562
JX
385 break;
386 }
387 }
1eeb4562 388
fc897b24
GN
389 if (err != 0 && fletcher_4_initialized) {
390 /* check all supported implementations */
391 for (i = 0; i < fletcher_4_supp_impls_cnt; i++) {
392 const char *name = fletcher_4_supp_impls[i]->name;
1eeb4562 393
fc897b24
GN
394 if (val_len == strlen(name) &&
395 strncmp(val, name, val_len) == 0) {
396 impl = i;
397 err = 0;
398 break;
399 }
400 }
401 }
1eeb4562 402
fc897b24
GN
403 if (err == 0) {
404 atomic_swap_32(&fletcher_4_impl_chosen, impl);
405 membar_producer();
406 }
407
408 return (err);
1eeb4562
JX
409}
410
411static inline const fletcher_4_ops_t *
412fletcher_4_impl_get(void)
413{
fc897b24
GN
414 fletcher_4_ops_t *ops = NULL;
415 const uint32_t impl = IMPL_READ(fletcher_4_impl_chosen);
416
417 switch (impl) {
418 case IMPL_FASTEST:
419 ASSERT(fletcher_4_initialized);
420 ops = &fletcher_4_fastest_impl;
421 break;
1eeb4562 422#if !defined(_KERNEL)
fc897b24
GN
423 case IMPL_CYCLE: {
424 ASSERT(fletcher_4_initialized);
425 ASSERT3U(fletcher_4_supp_impls_cnt, >, 0);
426
427 static uint32_t cycle_count = 0;
428 uint32_t idx = (++cycle_count) % fletcher_4_supp_impls_cnt;
429 ops = fletcher_4_supp_impls[idx];
1eeb4562 430 }
fc897b24 431 break;
1eeb4562 432#endif
fc897b24
GN
433 default:
434 ASSERT3U(fletcher_4_supp_impls_cnt, >, 0);
435 ASSERT3U(impl, <, fletcher_4_supp_impls_cnt);
436
437 ops = fletcher_4_supp_impls[impl];
438 break;
439 }
440
441 ASSERT3P(ops, !=, NULL);
442
443 return (ops);
444}
445
fc897b24 446static inline void
5bf703b8 447fletcher_4_native_impl(const void *buf, uint64_t size, zio_cksum_t *zcp)
fc897b24 448{
5bf703b8
GN
449 fletcher_4_ctx_t ctx;
450 const fletcher_4_ops_t *ops = fletcher_4_impl_get();
451
452 ops->init_native(&ctx);
453 ops->compute_native(&ctx, buf, size);
454 ops->fini_native(&ctx, zcp);
1eeb4562
JX
455}
456
3c67d83a 457/*ARGSUSED*/
1eeb4562 458void
3c67d83a
TH
459fletcher_4_native(const void *buf, uint64_t size,
460 const void *ctx_template, zio_cksum_t *zcp)
1eeb4562 461{
2fe36b0b 462 const uint64_t p2size = P2ALIGN(size, FLETCHER_MIN_SIMD_SIZE);
0dab2e84 463
fc897b24
GN
464 ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
465
5bf703b8 466 if (size == 0 || p2size == 0) {
fc897b24 467 ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
5bf703b8
GN
468
469 if (size > 0)
470 fletcher_4_scalar_native((fletcher_4_ctx_t *)zcp,
471 buf, size);
fc897b24 472 } else {
5bf703b8 473 fletcher_4_native_impl(buf, p2size, zcp);
1eeb4562 474
fc897b24 475 if (p2size < size)
5bf703b8
GN
476 fletcher_4_scalar_native((fletcher_4_ctx_t *)zcp,
477 (char *)buf + p2size, size - p2size);
fc897b24
GN
478 }
479}
480
481void
482fletcher_4_native_varsize(const void *buf, uint64_t size, zio_cksum_t *zcp)
483{
5bf703b8
GN
484 ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
485 fletcher_4_scalar_native((fletcher_4_ctx_t *)zcp, buf, size);
fc897b24
GN
486}
487
488static inline void
5bf703b8 489fletcher_4_byteswap_impl(const void *buf, uint64_t size, zio_cksum_t *zcp)
fc897b24 490{
5bf703b8
GN
491 fletcher_4_ctx_t ctx;
492 const fletcher_4_ops_t *ops = fletcher_4_impl_get();
493
494 ops->init_byteswap(&ctx);
495 ops->compute_byteswap(&ctx, buf, size);
496 ops->fini_byteswap(&ctx, zcp);
1eeb4562
JX
497}
498
3c67d83a 499/*ARGSUSED*/
1eeb4562 500void
3c67d83a
TH
501fletcher_4_byteswap(const void *buf, uint64_t size,
502 const void *ctx_template, zio_cksum_t *zcp)
1eeb4562 503{
2fe36b0b 504 const uint64_t p2size = P2ALIGN(size, FLETCHER_MIN_SIMD_SIZE);
0dab2e84 505
fc897b24
GN
506 ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
507
5bf703b8 508 if (size == 0 || p2size == 0) {
fc897b24 509 ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
5bf703b8
GN
510
511 if (size > 0)
512 fletcher_4_scalar_byteswap((fletcher_4_ctx_t *)zcp,
513 buf, size);
fc897b24 514 } else {
5bf703b8 515 fletcher_4_byteswap_impl(buf, p2size, zcp);
1eeb4562 516
fc897b24 517 if (p2size < size)
5bf703b8
GN
518 fletcher_4_scalar_byteswap((fletcher_4_ctx_t *)zcp,
519 (char *)buf + p2size, size - p2size);
fc897b24 520 }
1eeb4562
JX
521}
522
37f520db
GN
523/* Incremental Fletcher 4 */
524
5bf703b8
GN
525#define ZFS_FLETCHER_4_INC_MAX_SIZE (8ULL << 20)
526
37f520db
GN
527static inline void
528fletcher_4_incremental_combine(zio_cksum_t *zcp, const uint64_t size,
529 const zio_cksum_t *nzcp)
530{
531 const uint64_t c1 = size / sizeof (uint32_t);
532 const uint64_t c2 = c1 * (c1 + 1) / 2;
533 const uint64_t c3 = c2 * (c1 + 2) / 3;
534
5bf703b8
GN
535 /*
536 * Value of 'c3' overflows on buffer sizes close to 16MiB. For that
537 * reason we split incremental fletcher4 computation of large buffers
538 * to steps of (ZFS_FLETCHER_4_INC_MAX_SIZE) size.
539 */
540 ASSERT3U(size, <=, ZFS_FLETCHER_4_INC_MAX_SIZE);
541
37f520db
GN
542 zcp->zc_word[3] += nzcp->zc_word[3] + c1 * zcp->zc_word[2] +
543 c2 * zcp->zc_word[1] + c3 * zcp->zc_word[0];
544 zcp->zc_word[2] += nzcp->zc_word[2] + c1 * zcp->zc_word[1] +
545 c2 * zcp->zc_word[0];
546 zcp->zc_word[1] += nzcp->zc_word[1] + c1 * zcp->zc_word[0];
547 zcp->zc_word[0] += nzcp->zc_word[0];
548}
549
550static inline void
551fletcher_4_incremental_impl(boolean_t native, const void *buf, uint64_t size,
552 zio_cksum_t *zcp)
553{
37f520db
GN
554 while (size > 0) {
555 zio_cksum_t nzc;
5bf703b8 556 uint64_t len = MIN(size, ZFS_FLETCHER_4_INC_MAX_SIZE);
37f520db
GN
557
558 if (native)
559 fletcher_4_native(buf, len, NULL, &nzc);
560 else
561 fletcher_4_byteswap(buf, len, NULL, &nzc);
562
563 fletcher_4_incremental_combine(zcp, len, &nzc);
564
565 size -= len;
566 buf += len;
567 }
568}
569
a6255b7f
DQ
570int
571fletcher_4_incremental_native(void *buf, size_t size, void *data)
37f520db 572{
a6255b7f 573 zio_cksum_t *zcp = data;
5bf703b8
GN
574 /* Use scalar impl to directly update cksum of small blocks */
575 if (size < SPA_MINBLOCKSIZE)
576 fletcher_4_scalar_native((fletcher_4_ctx_t *)zcp, buf, size);
577 else
578 fletcher_4_incremental_impl(B_TRUE, buf, size, zcp);
a6255b7f 579 return (0);
37f520db
GN
580}
581
a6255b7f
DQ
582int
583fletcher_4_incremental_byteswap(void *buf, size_t size, void *data)
37f520db 584{
a6255b7f 585 zio_cksum_t *zcp = data;
5bf703b8
GN
586 /* Use scalar impl to directly update cksum of small blocks */
587 if (size < SPA_MINBLOCKSIZE)
588 fletcher_4_scalar_byteswap((fletcher_4_ctx_t *)zcp, buf, size);
589 else
590 fletcher_4_incremental_impl(B_FALSE, buf, size, zcp);
a6255b7f 591 return (0);
37f520db
GN
592}
593
46364cb2 594#if defined(_KERNEL)
37f520db
GN
595/* Fletcher 4 kstats */
596
fc897b24
GN
597static int
598fletcher_4_kstat_headers(char *buf, size_t size)
1eeb4562 599{
fc897b24
GN
600 ssize_t off = 0;
601
602 off += snprintf(buf + off, size, "%-17s", "implementation");
603 off += snprintf(buf + off, size - off, "%-15s", "native");
604 (void) snprintf(buf + off, size - off, "%-15s\n", "byteswap");
605
606 return (0);
1eeb4562
JX
607}
608
fc897b24
GN
609static int
610fletcher_4_kstat_data(char *buf, size_t size, void *data)
34dc7c2f 611{
fc897b24
GN
612 struct fletcher_4_kstat *fastest_stat =
613 &fletcher_4_stat_data[fletcher_4_supp_impls_cnt];
02730c33 614 struct fletcher_4_kstat *curr_stat = (struct fletcher_4_kstat *)data;
fc897b24
GN
615 ssize_t off = 0;
616
617 if (curr_stat == fastest_stat) {
618 off += snprintf(buf + off, size - off, "%-17s", "fastest");
619 off += snprintf(buf + off, size - off, "%-15s",
620 fletcher_4_supp_impls[fastest_stat->native]->name);
621 off += snprintf(buf + off, size - off, "%-15s\n",
622 fletcher_4_supp_impls[fastest_stat->byteswap]->name);
623 } else {
624 ptrdiff_t id = curr_stat - fletcher_4_stat_data;
625
626 off += snprintf(buf + off, size - off, "%-17s",
627 fletcher_4_supp_impls[id]->name);
628 off += snprintf(buf + off, size - off, "%-15llu",
02730c33 629 (u_longlong_t)curr_stat->native);
fc897b24 630 off += snprintf(buf + off, size - off, "%-15llu\n",
02730c33 631 (u_longlong_t)curr_stat->byteswap);
fc897b24
GN
632 }
633
634 return (0);
1eeb4562 635}
34dc7c2f 636
fc897b24
GN
637static void *
638fletcher_4_kstat_addr(kstat_t *ksp, loff_t n)
1eeb4562 639{
fc897b24
GN
640 if (n <= fletcher_4_supp_impls_cnt)
641 ksp->ks_private = (void *) (fletcher_4_stat_data + n);
642 else
643 ksp->ks_private = NULL;
644
645 return (ksp->ks_private);
646}
46364cb2 647#endif
fc897b24
GN
648
649#define FLETCHER_4_FASTEST_FN_COPY(type, src) \
650{ \
651 fletcher_4_fastest_impl.init_ ## type = src->init_ ## type; \
652 fletcher_4_fastest_impl.fini_ ## type = src->fini_ ## type; \
653 fletcher_4_fastest_impl.compute_ ## type = src->compute_ ## type; \
654}
655
656#define FLETCHER_4_BENCH_NS (MSEC2NSEC(50)) /* 50ms */
34dc7c2f 657
a6255b7f
DQ
658typedef void fletcher_checksum_func_t(const void *, uint64_t, const void *,
659 zio_cksum_t *);
660
fc897b24
GN
661static void
662fletcher_4_benchmark_impl(boolean_t native, char *data, uint64_t data_size)
663{
664
665 struct fletcher_4_kstat *fastest_stat =
666 &fletcher_4_stat_data[fletcher_4_supp_impls_cnt];
667 hrtime_t start;
668 uint64_t run_bw, run_time_ns, best_run = 0;
669 zio_cksum_t zc;
670 uint32_t i, l, sel_save = IMPL_READ(fletcher_4_impl_chosen);
671
a6255b7f
DQ
672
673 fletcher_checksum_func_t *fletcher_4_test = native ?
674 fletcher_4_native : fletcher_4_byteswap;
1eeb4562 675
fc897b24
GN
676 for (i = 0; i < fletcher_4_supp_impls_cnt; i++) {
677 struct fletcher_4_kstat *stat = &fletcher_4_stat_data[i];
678 uint64_t run_count = 0;
1eeb4562 679
fc897b24
GN
680 /* temporary set an implementation */
681 fletcher_4_impl_chosen = i;
1eeb4562
JX
682
683 kpreempt_disable();
684 start = gethrtime();
1eeb4562 685 do {
fc897b24 686 for (l = 0; l < 32; l++, run_count++)
3c67d83a 687 fletcher_4_test(data, data_size, NULL, &zc);
fc897b24
GN
688
689 run_time_ns = gethrtime() - start;
690 } while (run_time_ns < FLETCHER_4_BENCH_NS);
1eeb4562
JX
691 kpreempt_enable();
692
fc897b24
GN
693 run_bw = data_size * run_count * NANOSEC;
694 run_bw /= run_time_ns; /* B/s */
695
696 if (native)
697 stat->native = run_bw;
698 else
699 stat->byteswap = run_bw;
700
701 if (run_bw > best_run) {
702 best_run = run_bw;
703
704 if (native) {
705 fastest_stat->native = i;
706 FLETCHER_4_FASTEST_FN_COPY(native,
707 fletcher_4_supp_impls[i]);
708 } else {
709 fastest_stat->byteswap = i;
710 FLETCHER_4_FASTEST_FN_COPY(byteswap,
711 fletcher_4_supp_impls[i]);
712 }
1eeb4562 713 }
fc897b24
GN
714 }
715
716 /* restore original selection */
717 atomic_swap_32(&fletcher_4_impl_chosen, sel_save);
718}
1eeb4562 719
fc897b24
GN
720void
721fletcher_4_init(void)
722{
723 static const size_t data_size = 1 << SPA_OLD_MAXBLOCKSHIFT; /* 128kiB */
724 fletcher_4_ops_t *curr_impl;
725 char *databuf;
726 int i, c;
727
728 /* move supported impl into fletcher_4_supp_impls */
729 for (i = 0, c = 0; i < ARRAY_SIZE(fletcher_4_impls); i++) {
02730c33 730 curr_impl = (fletcher_4_ops_t *)fletcher_4_impls[i];
fc897b24
GN
731
732 if (curr_impl->valid && curr_impl->valid())
733 fletcher_4_supp_impls[c++] = curr_impl;
34dc7c2f 734 }
fc897b24
GN
735 membar_producer(); /* complete fletcher_4_supp_impls[] init */
736 fletcher_4_supp_impls_cnt = c; /* number of supported impl */
34dc7c2f 737
fc897b24
GN
738#if !defined(_KERNEL)
739 /* Skip benchmarking and use last implementation as fastest */
740 memcpy(&fletcher_4_fastest_impl,
741 fletcher_4_supp_impls[fletcher_4_supp_impls_cnt-1],
742 sizeof (fletcher_4_fastest_impl));
743 fletcher_4_fastest_impl.name = "fastest";
744 membar_producer();
1eeb4562 745
fc897b24 746 fletcher_4_initialized = B_TRUE;
fc897b24
GN
747 return;
748#endif
749 /* Benchmark all supported implementations */
750 databuf = vmem_alloc(data_size, KM_SLEEP);
751 for (i = 0; i < data_size / sizeof (uint64_t); i++)
752 ((uint64_t *)databuf)[i] = (uintptr_t)(databuf+i); /* warm-up */
753
754 fletcher_4_benchmark_impl(B_FALSE, databuf, data_size);
755 fletcher_4_benchmark_impl(B_TRUE, databuf, data_size);
756
757 vmem_free(databuf, data_size);
758
46364cb2 759#if defined(_KERNEL)
fc897b24
GN
760 /* install kstats for all implementations */
761 fletcher_4_kstat = kstat_create("zfs", 0, "fletcher_4_bench", "misc",
02730c33 762 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
1eeb4562 763 if (fletcher_4_kstat != NULL) {
fc897b24
GN
764 fletcher_4_kstat->ks_data = NULL;
765 fletcher_4_kstat->ks_ndata = UINT32_MAX;
766 kstat_set_raw_ops(fletcher_4_kstat,
767 fletcher_4_kstat_headers,
768 fletcher_4_kstat_data,
769 fletcher_4_kstat_addr);
1eeb4562
JX
770 kstat_install(fletcher_4_kstat);
771 }
46364cb2 772#endif
fc897b24
GN
773
774 /* Finish initialization */
775 fletcher_4_initialized = B_TRUE;
1eeb4562
JX
776}
777
778void
779fletcher_4_fini(void)
780{
46364cb2 781#if defined(_KERNEL)
1eeb4562
JX
782 if (fletcher_4_kstat != NULL) {
783 kstat_delete(fletcher_4_kstat);
784 fletcher_4_kstat = NULL;
785 }
46364cb2 786#endif
34dc7c2f 787}
c28b2279 788
2fe36b0b
DQ
789/* ABD adapters */
790
791static void
792abd_fletcher_4_init(zio_abd_checksum_data_t *cdp)
793{
794 const fletcher_4_ops_t *ops = fletcher_4_impl_get();
795 cdp->acd_private = (void *) ops;
796
797 if (cdp->acd_byteorder == ZIO_CHECKSUM_NATIVE)
798 ops->init_native(cdp->acd_ctx);
799 else
800 ops->init_byteswap(cdp->acd_ctx);
801}
802
803static void
804abd_fletcher_4_fini(zio_abd_checksum_data_t *cdp)
805{
806 fletcher_4_ops_t *ops = (fletcher_4_ops_t *)cdp->acd_private;
807
808 ASSERT(ops);
809
810 if (cdp->acd_byteorder == ZIO_CHECKSUM_NATIVE)
811 ops->fini_native(cdp->acd_ctx, cdp->acd_zcp);
812 else
813 ops->fini_byteswap(cdp->acd_ctx, cdp->acd_zcp);
814}
815
816static void
817abd_fletcher_4_simd2scalar(boolean_t native, void *data, size_t size,
818 zio_abd_checksum_data_t *cdp)
819{
820 zio_cksum_t *zcp = cdp->acd_zcp;
821
822 ASSERT3U(size, <, FLETCHER_MIN_SIMD_SIZE);
823
824 abd_fletcher_4_fini(cdp);
825 cdp->acd_private = (void *)&fletcher_4_scalar_ops;
826
827 if (native)
828 fletcher_4_incremental_native(data, size, zcp);
829 else
830 fletcher_4_incremental_byteswap(data, size, zcp);
831}
832
833static int
834abd_fletcher_4_iter(void *data, size_t size, void *private)
835{
836 zio_abd_checksum_data_t *cdp = (zio_abd_checksum_data_t *)private;
837 fletcher_4_ctx_t *ctx = cdp->acd_ctx;
838 fletcher_4_ops_t *ops = (fletcher_4_ops_t *)cdp->acd_private;
839 boolean_t native = cdp->acd_byteorder == ZIO_CHECKSUM_NATIVE;
840 uint64_t asize = P2ALIGN(size, FLETCHER_MIN_SIMD_SIZE);
841
842 ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
843
844 if (asize > 0) {
845 if (native)
846 ops->compute_native(ctx, data, asize);
847 else
848 ops->compute_byteswap(ctx, data, asize);
849
850 size -= asize;
851 data = (char *)data + asize;
852 }
853
854 if (size > 0) {
855 ASSERT3U(size, <, FLETCHER_MIN_SIMD_SIZE);
856 /* At this point we have to switch to scalar impl */
857 abd_fletcher_4_simd2scalar(native, data, size, cdp);
858 }
859
860 return (0);
861}
862
863zio_abd_checksum_func_t fletcher_4_abd_ops = {
864 .acf_init = abd_fletcher_4_init,
865 .acf_fini = abd_fletcher_4_fini,
866 .acf_iter = abd_fletcher_4_iter
867};
868
869
93ce2b4c 870#if defined(_KERNEL)
9cc1844a 871#include <linux/mod_compat.h>
1eeb4562
JX
872
873static int
9cc1844a 874fletcher_4_param_get(char *buffer, zfs_kernel_param_t *unused)
1eeb4562 875{
fc897b24
GN
876 const uint32_t impl = IMPL_READ(fletcher_4_impl_chosen);
877 char *fmt;
1eeb4562
JX
878 int i, cnt = 0;
879
fc897b24
GN
880 /* list fastest */
881 fmt = (impl == IMPL_FASTEST) ? "[%s] " : "%s ";
882 cnt += sprintf(buffer + cnt, fmt, "fastest");
1eeb4562 883
fc897b24
GN
884 /* list all supported implementations */
885 for (i = 0; i < fletcher_4_supp_impls_cnt; i++) {
886 fmt = (i == impl) ? "[%s] " : "%s ";
887 cnt += sprintf(buffer + cnt, fmt,
888 fletcher_4_supp_impls[i]->name);
1eeb4562
JX
889 }
890
891 return (cnt);
892}
893
894static int
9cc1844a 895fletcher_4_param_set(const char *val, zfs_kernel_param_t *unused)
1eeb4562
JX
896{
897 return (fletcher_4_impl_set(val));
898}
899
900/*
901 * Choose a fletcher 4 implementation in ZFS.
fc897b24 902 * Users can choose "cycle" to exercise all implementations, but this is
1eeb4562
JX
903 * for testing purpose therefore it can only be set in user space.
904 */
905module_param_call(zfs_fletcher_4_impl,
906 fletcher_4_param_set, fletcher_4_param_get, NULL, 0644);
fc897b24 907MODULE_PARM_DESC(zfs_fletcher_4_impl, "Select fletcher 4 implementation.");
1eeb4562 908
a6255b7f
DQ
909EXPORT_SYMBOL(fletcher_init);
910EXPORT_SYMBOL(fletcher_2_incremental_native);
911EXPORT_SYMBOL(fletcher_2_incremental_byteswap);
1eeb4562
JX
912EXPORT_SYMBOL(fletcher_4_init);
913EXPORT_SYMBOL(fletcher_4_fini);
c28b2279
BB
914EXPORT_SYMBOL(fletcher_2_native);
915EXPORT_SYMBOL(fletcher_2_byteswap);
916EXPORT_SYMBOL(fletcher_4_native);
fc897b24 917EXPORT_SYMBOL(fletcher_4_native_varsize);
c28b2279
BB
918EXPORT_SYMBOL(fletcher_4_byteswap);
919EXPORT_SYMBOL(fletcher_4_incremental_native);
920EXPORT_SYMBOL(fletcher_4_incremental_byteswap);
2fe36b0b 921EXPORT_SYMBOL(fletcher_4_abd_ops);
c28b2279 922#endif