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