xref: /freebsd/sys/contrib/openzfs/module/zstd/zfs_zstd.c (revision 36639c3942f587e652d2aba6a71ad45b64c2ce47)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * BSD 3-Clause New License (https://spdx.org/licenses/BSD-3-Clause.html)
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * Redistribution and use in source and binary forms, with or without
5eda14cbcSMatt Macy  * modification, are permitted provided that the following conditions are met:
6eda14cbcSMatt Macy  *
7eda14cbcSMatt Macy  * 1. Redistributions of source code must retain the above copyright notice,
8eda14cbcSMatt Macy  * this list of conditions and the following disclaimer.
9eda14cbcSMatt Macy  *
10eda14cbcSMatt Macy  * 2. Redistributions in binary form must reproduce the above copyright notice,
11eda14cbcSMatt Macy  * this list of conditions and the following disclaimer in the documentation
12eda14cbcSMatt Macy  * and/or other materials provided with the distribution.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * 3. Neither the name of the copyright holder nor the names of its
15eda14cbcSMatt Macy  * contributors may be used to endorse or promote products derived from this
16eda14cbcSMatt Macy  * software without specific prior written permission.
17eda14cbcSMatt Macy  *
18eda14cbcSMatt Macy  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19eda14cbcSMatt Macy  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20eda14cbcSMatt Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21eda14cbcSMatt Macy  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22eda14cbcSMatt Macy  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23eda14cbcSMatt Macy  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24eda14cbcSMatt Macy  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25eda14cbcSMatt Macy  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26eda14cbcSMatt Macy  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27eda14cbcSMatt Macy  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28eda14cbcSMatt Macy  * POSSIBILITY OF SUCH DAMAGE.
29eda14cbcSMatt Macy  */
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy /*
32eda14cbcSMatt Macy  * Copyright (c) 2016-2018, Klara Inc.
33eda14cbcSMatt Macy  * Copyright (c) 2016-2018, Allan Jude
34eda14cbcSMatt Macy  * Copyright (c) 2018-2020, Sebastian Gottschall
35eda14cbcSMatt Macy  * Copyright (c) 2019-2020, Michael Niewöhner
36eda14cbcSMatt Macy  * Copyright (c) 2020, The FreeBSD Foundation [1]
37eda14cbcSMatt Macy  *
38eda14cbcSMatt Macy  * [1] Portions of this software were developed by Allan Jude
39eda14cbcSMatt Macy  *     under sponsorship from the FreeBSD Foundation.
40eda14cbcSMatt Macy  */
41eda14cbcSMatt Macy 
42eda14cbcSMatt Macy #include <sys/param.h>
43eda14cbcSMatt Macy #include <sys/sysmacros.h>
44eda14cbcSMatt Macy #include <sys/zfs_context.h>
45eda14cbcSMatt Macy #include <sys/zio_compress.h>
46eda14cbcSMatt Macy #include <sys/spa.h>
47eda14cbcSMatt Macy #include <sys/zstd/zstd.h>
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy #define	ZSTD_STATIC_LINKING_ONLY
50eda14cbcSMatt Macy #include "lib/zstd.h"
51eda14cbcSMatt Macy #include "lib/zstd_errors.h"
52eda14cbcSMatt Macy 
53eda14cbcSMatt Macy kstat_t *zstd_ksp = NULL;
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy typedef struct zstd_stats {
56eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_alloc_fail;
57eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_alloc_fallback;
58eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_com_alloc_fail;
59eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_dec_alloc_fail;
60eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_com_inval;
61eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_dec_inval;
62eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_dec_header_inval;
63eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_com_fail;
64eda14cbcSMatt Macy 	kstat_named_t	zstd_stat_dec_fail;
654a58b4abSMateusz Guzik 	kstat_named_t	zstd_stat_buffers;
664a58b4abSMateusz Guzik 	kstat_named_t	zstd_stat_size;
67eda14cbcSMatt Macy } zstd_stats_t;
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy static zstd_stats_t zstd_stats = {
70eda14cbcSMatt Macy 	{ "alloc_fail",			KSTAT_DATA_UINT64 },
71eda14cbcSMatt Macy 	{ "alloc_fallback",		KSTAT_DATA_UINT64 },
72eda14cbcSMatt Macy 	{ "compress_alloc_fail",	KSTAT_DATA_UINT64 },
73eda14cbcSMatt Macy 	{ "decompress_alloc_fail",	KSTAT_DATA_UINT64 },
74eda14cbcSMatt Macy 	{ "compress_level_invalid",	KSTAT_DATA_UINT64 },
75eda14cbcSMatt Macy 	{ "decompress_level_invalid",	KSTAT_DATA_UINT64 },
76eda14cbcSMatt Macy 	{ "decompress_header_invalid",	KSTAT_DATA_UINT64 },
77eda14cbcSMatt Macy 	{ "compress_failed",		KSTAT_DATA_UINT64 },
78eda14cbcSMatt Macy 	{ "decompress_failed",		KSTAT_DATA_UINT64 },
794a58b4abSMateusz Guzik 	{ "buffers",			KSTAT_DATA_UINT64 },
804a58b4abSMateusz Guzik 	{ "size",			KSTAT_DATA_UINT64 },
81eda14cbcSMatt Macy };
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy /* Enums describing the allocator type specified by kmem_type in zstd_kmem */
84eda14cbcSMatt Macy enum zstd_kmem_type {
85eda14cbcSMatt Macy 	ZSTD_KMEM_UNKNOWN = 0,
86eda14cbcSMatt Macy 	/* Allocation type using kmem_vmalloc */
87eda14cbcSMatt Macy 	ZSTD_KMEM_DEFAULT,
88eda14cbcSMatt Macy 	/* Pool based allocation using mempool_alloc */
89eda14cbcSMatt Macy 	ZSTD_KMEM_POOL,
90eda14cbcSMatt Macy 	/* Reserved fallback memory for decompression only */
91eda14cbcSMatt Macy 	ZSTD_KMEM_DCTX,
92eda14cbcSMatt Macy 	ZSTD_KMEM_COUNT,
93eda14cbcSMatt Macy };
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy /* Structure for pooled memory objects */
96eda14cbcSMatt Macy struct zstd_pool {
97eda14cbcSMatt Macy 	void *mem;
98eda14cbcSMatt Macy 	size_t size;
99eda14cbcSMatt Macy 	kmutex_t barrier;
100eda14cbcSMatt Macy 	hrtime_t timeout;
101eda14cbcSMatt Macy };
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy /* Global structure for handling memory allocations */
104eda14cbcSMatt Macy struct zstd_kmem {
105eda14cbcSMatt Macy 	enum zstd_kmem_type kmem_type;
106eda14cbcSMatt Macy 	size_t kmem_size;
107eda14cbcSMatt Macy 	struct zstd_pool *pool;
108eda14cbcSMatt Macy };
109eda14cbcSMatt Macy 
110eda14cbcSMatt Macy /* Fallback memory structure used for decompression only if memory runs out */
111eda14cbcSMatt Macy struct zstd_fallback_mem {
112eda14cbcSMatt Macy 	size_t mem_size;
113eda14cbcSMatt Macy 	void *mem;
114eda14cbcSMatt Macy 	kmutex_t barrier;
115eda14cbcSMatt Macy };
116eda14cbcSMatt Macy 
117eda14cbcSMatt Macy struct zstd_levelmap {
118eda14cbcSMatt Macy 	int16_t zstd_level;
119eda14cbcSMatt Macy 	enum zio_zstd_levels level;
120eda14cbcSMatt Macy };
121eda14cbcSMatt Macy 
122eda14cbcSMatt Macy /*
123eda14cbcSMatt Macy  * ZSTD memory handlers
124eda14cbcSMatt Macy  *
125eda14cbcSMatt Macy  * For decompression we use a different handler which also provides fallback
126eda14cbcSMatt Macy  * memory allocation in case memory runs out.
127eda14cbcSMatt Macy  *
128eda14cbcSMatt Macy  * The ZSTD handlers were split up for the most simplified implementation.
129eda14cbcSMatt Macy  */
130eda14cbcSMatt Macy static void *zstd_alloc(void *opaque, size_t size);
131eda14cbcSMatt Macy static void *zstd_dctx_alloc(void *opaque, size_t size);
132eda14cbcSMatt Macy static void zstd_free(void *opaque, void *ptr);
133eda14cbcSMatt Macy 
134eda14cbcSMatt Macy /* Compression memory handler */
135eda14cbcSMatt Macy static const ZSTD_customMem zstd_malloc = {
136eda14cbcSMatt Macy 	zstd_alloc,
137eda14cbcSMatt Macy 	zstd_free,
138eda14cbcSMatt Macy 	NULL,
139eda14cbcSMatt Macy };
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy /* Decompression memory handler */
142eda14cbcSMatt Macy static const ZSTD_customMem zstd_dctx_malloc = {
143eda14cbcSMatt Macy 	zstd_dctx_alloc,
144eda14cbcSMatt Macy 	zstd_free,
145eda14cbcSMatt Macy 	NULL,
146eda14cbcSMatt Macy };
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy /* Level map for converting ZFS internal levels to ZSTD levels and vice versa */
149eda14cbcSMatt Macy static struct zstd_levelmap zstd_levels[] = {
150eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_1, ZIO_ZSTD_LEVEL_1},
151eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_2, ZIO_ZSTD_LEVEL_2},
152eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_3, ZIO_ZSTD_LEVEL_3},
153eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_4, ZIO_ZSTD_LEVEL_4},
154eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_5, ZIO_ZSTD_LEVEL_5},
155eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_6, ZIO_ZSTD_LEVEL_6},
156eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_7, ZIO_ZSTD_LEVEL_7},
157eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_8, ZIO_ZSTD_LEVEL_8},
158eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_9, ZIO_ZSTD_LEVEL_9},
159eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_10, ZIO_ZSTD_LEVEL_10},
160eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_11, ZIO_ZSTD_LEVEL_11},
161eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_12, ZIO_ZSTD_LEVEL_12},
162eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_13, ZIO_ZSTD_LEVEL_13},
163eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_14, ZIO_ZSTD_LEVEL_14},
164eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_15, ZIO_ZSTD_LEVEL_15},
165eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_16, ZIO_ZSTD_LEVEL_16},
166eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_17, ZIO_ZSTD_LEVEL_17},
167eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_18, ZIO_ZSTD_LEVEL_18},
168eda14cbcSMatt Macy 	{ZIO_ZSTD_LEVEL_19, ZIO_ZSTD_LEVEL_19},
169eda14cbcSMatt Macy 	{-1, ZIO_ZSTD_LEVEL_FAST_1},
170eda14cbcSMatt Macy 	{-2, ZIO_ZSTD_LEVEL_FAST_2},
171eda14cbcSMatt Macy 	{-3, ZIO_ZSTD_LEVEL_FAST_3},
172eda14cbcSMatt Macy 	{-4, ZIO_ZSTD_LEVEL_FAST_4},
173eda14cbcSMatt Macy 	{-5, ZIO_ZSTD_LEVEL_FAST_5},
174eda14cbcSMatt Macy 	{-6, ZIO_ZSTD_LEVEL_FAST_6},
175eda14cbcSMatt Macy 	{-7, ZIO_ZSTD_LEVEL_FAST_7},
176eda14cbcSMatt Macy 	{-8, ZIO_ZSTD_LEVEL_FAST_8},
177eda14cbcSMatt Macy 	{-9, ZIO_ZSTD_LEVEL_FAST_9},
178eda14cbcSMatt Macy 	{-10, ZIO_ZSTD_LEVEL_FAST_10},
179eda14cbcSMatt Macy 	{-20, ZIO_ZSTD_LEVEL_FAST_20},
180eda14cbcSMatt Macy 	{-30, ZIO_ZSTD_LEVEL_FAST_30},
181eda14cbcSMatt Macy 	{-40, ZIO_ZSTD_LEVEL_FAST_40},
182eda14cbcSMatt Macy 	{-50, ZIO_ZSTD_LEVEL_FAST_50},
183eda14cbcSMatt Macy 	{-60, ZIO_ZSTD_LEVEL_FAST_60},
184eda14cbcSMatt Macy 	{-70, ZIO_ZSTD_LEVEL_FAST_70},
185eda14cbcSMatt Macy 	{-80, ZIO_ZSTD_LEVEL_FAST_80},
186eda14cbcSMatt Macy 	{-90, ZIO_ZSTD_LEVEL_FAST_90},
187eda14cbcSMatt Macy 	{-100, ZIO_ZSTD_LEVEL_FAST_100},
188eda14cbcSMatt Macy 	{-500, ZIO_ZSTD_LEVEL_FAST_500},
189eda14cbcSMatt Macy 	{-1000, ZIO_ZSTD_LEVEL_FAST_1000},
190eda14cbcSMatt Macy };
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy /*
193eda14cbcSMatt Macy  * This variable represents the maximum count of the pool based on the number
194eda14cbcSMatt Macy  * of CPUs plus some buffer. We default to cpu count * 4, see init_zstd.
195eda14cbcSMatt Macy  */
196eda14cbcSMatt Macy static int pool_count = 16;
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy #define	ZSTD_POOL_MAX		pool_count
199eda14cbcSMatt Macy #define	ZSTD_POOL_TIMEOUT	60 * 2
200eda14cbcSMatt Macy 
201eda14cbcSMatt Macy static struct zstd_fallback_mem zstd_dctx_fallback;
202eda14cbcSMatt Macy static struct zstd_pool *zstd_mempool_cctx;
203eda14cbcSMatt Macy static struct zstd_pool *zstd_mempool_dctx;
204eda14cbcSMatt Macy 
205eda14cbcSMatt Macy /*
206eda14cbcSMatt Macy  * Try to get a cached allocated buffer from memory pool or allocate a new one
207eda14cbcSMatt Macy  * if necessary. If a object is older than 2 minutes and does not fit the
208eda14cbcSMatt Macy  * requested size, it will be released and a new cached entry will be allocated.
209eda14cbcSMatt Macy  * If other pooled objects are detected without being used for 2 minutes, they
210eda14cbcSMatt Macy  * will be released, too.
211eda14cbcSMatt Macy  *
212eda14cbcSMatt Macy  * The concept is that high frequency memory allocations of bigger objects are
213eda14cbcSMatt Macy  * expensive. So if a lot of work is going on, allocations will be kept for a
214eda14cbcSMatt Macy  * while and can be reused in that time frame.
215eda14cbcSMatt Macy  *
216eda14cbcSMatt Macy  * The scheduled release will be updated every time a object is reused.
217eda14cbcSMatt Macy  */
218eda14cbcSMatt Macy static void *
219eda14cbcSMatt Macy zstd_mempool_alloc(struct zstd_pool *zstd_mempool, size_t size)
220eda14cbcSMatt Macy {
221eda14cbcSMatt Macy 	struct zstd_pool *pool;
222eda14cbcSMatt Macy 	struct zstd_kmem *mem = NULL;
223eda14cbcSMatt Macy 
224eda14cbcSMatt Macy 	if (!zstd_mempool) {
225eda14cbcSMatt Macy 		return (NULL);
226eda14cbcSMatt Macy 	}
227eda14cbcSMatt Macy 
228eda14cbcSMatt Macy 	/* Seek for preallocated memory slot and free obsolete slots */
229eda14cbcSMatt Macy 	for (int i = 0; i < ZSTD_POOL_MAX; i++) {
230eda14cbcSMatt Macy 		pool = &zstd_mempool[i];
231eda14cbcSMatt Macy 		/*
232eda14cbcSMatt Macy 		 * This lock is simply a marker for a pool object beeing in use.
233eda14cbcSMatt Macy 		 * If it's already hold, it will be skipped.
234eda14cbcSMatt Macy 		 *
235eda14cbcSMatt Macy 		 * We need to create it before checking it to avoid race
236eda14cbcSMatt Macy 		 * conditions caused by running in a threaded context.
237eda14cbcSMatt Macy 		 *
238eda14cbcSMatt Macy 		 * The lock is later released by zstd_mempool_free.
239eda14cbcSMatt Macy 		 */
240eda14cbcSMatt Macy 		if (mutex_tryenter(&pool->barrier)) {
241eda14cbcSMatt Macy 			/*
242eda14cbcSMatt Macy 			 * Check if objects fits the size, if so we take it and
243eda14cbcSMatt Macy 			 * update the timestamp.
244eda14cbcSMatt Macy 			 */
245c40487d4SMatt Macy 			if (size && !mem && pool->mem && size <= pool->size) {
246eda14cbcSMatt Macy 				pool->timeout = gethrestime_sec() +
247eda14cbcSMatt Macy 				    ZSTD_POOL_TIMEOUT;
248eda14cbcSMatt Macy 				mem = pool->mem;
249eda14cbcSMatt Macy 				continue;
250eda14cbcSMatt Macy 			}
251eda14cbcSMatt Macy 
252eda14cbcSMatt Macy 			/* Free memory if unused object older than 2 minutes */
253eda14cbcSMatt Macy 			if (pool->mem && gethrestime_sec() > pool->timeout) {
254eda14cbcSMatt Macy 				vmem_free(pool->mem, pool->size);
2554a58b4abSMateusz Guzik 				ZSTDSTAT_SUB(zstd_stat_buffers, 1);
2564a58b4abSMateusz Guzik 				ZSTDSTAT_SUB(zstd_stat_size, pool->size);
257eda14cbcSMatt Macy 				pool->mem = NULL;
258eda14cbcSMatt Macy 				pool->size = 0;
259eda14cbcSMatt Macy 				pool->timeout = 0;
260eda14cbcSMatt Macy 			}
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 			mutex_exit(&pool->barrier);
263eda14cbcSMatt Macy 		}
264eda14cbcSMatt Macy 	}
265eda14cbcSMatt Macy 
266c40487d4SMatt Macy 	if (!size || mem) {
267eda14cbcSMatt Macy 		return (mem);
268eda14cbcSMatt Macy 	}
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	/*
271eda14cbcSMatt Macy 	 * If no preallocated slot was found, try to fill in a new one.
272eda14cbcSMatt Macy 	 *
273eda14cbcSMatt Macy 	 * We run a similar algorithm twice here to avoid pool fragmentation.
274eda14cbcSMatt Macy 	 * The first one may generate holes in the list if objects get released.
275eda14cbcSMatt Macy 	 * We always make sure that these holes get filled instead of adding new
276eda14cbcSMatt Macy 	 * allocations constantly at the end.
277eda14cbcSMatt Macy 	 */
278eda14cbcSMatt Macy 	for (int i = 0; i < ZSTD_POOL_MAX; i++) {
279eda14cbcSMatt Macy 		pool = &zstd_mempool[i];
280eda14cbcSMatt Macy 		if (mutex_tryenter(&pool->barrier)) {
281eda14cbcSMatt Macy 			/* Object is free, try to allocate new one */
282eda14cbcSMatt Macy 			if (!pool->mem) {
283eda14cbcSMatt Macy 				mem = vmem_alloc(size, KM_SLEEP);
2844a58b4abSMateusz Guzik 				if (mem) {
2854a58b4abSMateusz Guzik 					ZSTDSTAT_ADD(zstd_stat_buffers, 1);
2864a58b4abSMateusz Guzik 					ZSTDSTAT_ADD(zstd_stat_size, size);
287eda14cbcSMatt Macy 					pool->mem = mem;
2884a58b4abSMateusz Guzik 					pool->size = size;
289eda14cbcSMatt Macy 					/* Keep track for later release */
290eda14cbcSMatt Macy 					mem->pool = pool;
291eda14cbcSMatt Macy 					mem->kmem_type = ZSTD_KMEM_POOL;
292eda14cbcSMatt Macy 					mem->kmem_size = size;
293eda14cbcSMatt Macy 				}
294eda14cbcSMatt Macy 			}
295eda14cbcSMatt Macy 
296eda14cbcSMatt Macy 			if (size <= pool->size) {
297eda14cbcSMatt Macy 				/* Update timestamp */
298eda14cbcSMatt Macy 				pool->timeout = gethrestime_sec() +
299eda14cbcSMatt Macy 				    ZSTD_POOL_TIMEOUT;
300eda14cbcSMatt Macy 
301eda14cbcSMatt Macy 				return (pool->mem);
302eda14cbcSMatt Macy 			}
303eda14cbcSMatt Macy 
304eda14cbcSMatt Macy 			mutex_exit(&pool->barrier);
305eda14cbcSMatt Macy 		}
306eda14cbcSMatt Macy 	}
307eda14cbcSMatt Macy 
308eda14cbcSMatt Macy 	/*
309eda14cbcSMatt Macy 	 * If the pool is full or the allocation failed, try lazy allocation
310eda14cbcSMatt Macy 	 * instead.
311eda14cbcSMatt Macy 	 */
312eda14cbcSMatt Macy 	if (!mem) {
313eda14cbcSMatt Macy 		mem = vmem_alloc(size, KM_NOSLEEP);
314eda14cbcSMatt Macy 		if (mem) {
315eda14cbcSMatt Macy 			mem->pool = NULL;
316eda14cbcSMatt Macy 			mem->kmem_type = ZSTD_KMEM_DEFAULT;
317eda14cbcSMatt Macy 			mem->kmem_size = size;
318eda14cbcSMatt Macy 		}
319eda14cbcSMatt Macy 	}
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy 	return (mem);
322eda14cbcSMatt Macy }
323eda14cbcSMatt Macy 
324eda14cbcSMatt Macy /* Mark object as released by releasing the barrier mutex */
325eda14cbcSMatt Macy static void
326eda14cbcSMatt Macy zstd_mempool_free(struct zstd_kmem *z)
327eda14cbcSMatt Macy {
328eda14cbcSMatt Macy 	mutex_exit(&z->pool->barrier);
329eda14cbcSMatt Macy }
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy /* Convert ZFS internal enum to ZSTD level */
332eda14cbcSMatt Macy static int
333eda14cbcSMatt Macy zstd_enum_to_level(enum zio_zstd_levels level, int16_t *zstd_level)
334eda14cbcSMatt Macy {
335eda14cbcSMatt Macy 	if (level > 0 && level <= ZIO_ZSTD_LEVEL_19) {
336eda14cbcSMatt Macy 		*zstd_level = zstd_levels[level - 1].zstd_level;
337eda14cbcSMatt Macy 		return (0);
338eda14cbcSMatt Macy 	}
339eda14cbcSMatt Macy 	if (level >= ZIO_ZSTD_LEVEL_FAST_1 &&
340eda14cbcSMatt Macy 	    level <= ZIO_ZSTD_LEVEL_FAST_1000) {
341eda14cbcSMatt Macy 		*zstd_level = zstd_levels[level - ZIO_ZSTD_LEVEL_FAST_1
342eda14cbcSMatt Macy 		    + ZIO_ZSTD_LEVEL_19].zstd_level;
343eda14cbcSMatt Macy 		return (0);
344eda14cbcSMatt Macy 	}
345eda14cbcSMatt Macy 
346eda14cbcSMatt Macy 	/* Invalid/unknown zfs compression enum - this should never happen. */
347eda14cbcSMatt Macy 	return (1);
348eda14cbcSMatt Macy }
349eda14cbcSMatt Macy 
350eda14cbcSMatt Macy /* Compress block using zstd */
351eda14cbcSMatt Macy size_t
352eda14cbcSMatt Macy zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len,
353eda14cbcSMatt Macy     int level)
354eda14cbcSMatt Macy {
355eda14cbcSMatt Macy 	size_t c_len;
356eda14cbcSMatt Macy 	int16_t zstd_level;
357eda14cbcSMatt Macy 	zfs_zstdhdr_t *hdr;
358eda14cbcSMatt Macy 	ZSTD_CCtx *cctx;
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy 	hdr = (zfs_zstdhdr_t *)d_start;
361eda14cbcSMatt Macy 
362eda14cbcSMatt Macy 	/* Skip compression if the specified level is invalid */
363eda14cbcSMatt Macy 	if (zstd_enum_to_level(level, &zstd_level)) {
364eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_com_inval);
365eda14cbcSMatt Macy 		return (s_len);
366eda14cbcSMatt Macy 	}
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 	ASSERT3U(d_len, >=, sizeof (*hdr));
369eda14cbcSMatt Macy 	ASSERT3U(d_len, <=, s_len);
370eda14cbcSMatt Macy 	ASSERT3U(zstd_level, !=, 0);
371eda14cbcSMatt Macy 
372eda14cbcSMatt Macy 	cctx = ZSTD_createCCtx_advanced(zstd_malloc);
373eda14cbcSMatt Macy 
374eda14cbcSMatt Macy 	/*
375eda14cbcSMatt Macy 	 * Out of kernel memory, gently fall through - this will disable
376eda14cbcSMatt Macy 	 * compression in zio_compress_data
377eda14cbcSMatt Macy 	 */
378eda14cbcSMatt Macy 	if (!cctx) {
379eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_com_alloc_fail);
380eda14cbcSMatt Macy 		return (s_len);
381eda14cbcSMatt Macy 	}
382eda14cbcSMatt Macy 
383eda14cbcSMatt Macy 	/* Set the compression level */
384eda14cbcSMatt Macy 	ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, zstd_level);
385eda14cbcSMatt Macy 
386eda14cbcSMatt Macy 	/* Use the "magicless" zstd header which saves us 4 header bytes */
387eda14cbcSMatt Macy 	ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless);
388eda14cbcSMatt Macy 
389eda14cbcSMatt Macy 	/*
390eda14cbcSMatt Macy 	 * Disable redundant checksum calculation and content size storage since
391eda14cbcSMatt Macy 	 * this is already done by ZFS itself.
392eda14cbcSMatt Macy 	 */
393eda14cbcSMatt Macy 	ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0);
394eda14cbcSMatt Macy 	ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0);
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy 	c_len = ZSTD_compress2(cctx,
397eda14cbcSMatt Macy 	    hdr->data,
398eda14cbcSMatt Macy 	    d_len - sizeof (*hdr),
399eda14cbcSMatt Macy 	    s_start, s_len);
400eda14cbcSMatt Macy 
401eda14cbcSMatt Macy 	ZSTD_freeCCtx(cctx);
402eda14cbcSMatt Macy 
403eda14cbcSMatt Macy 	/* Error in the compression routine, disable compression. */
404eda14cbcSMatt Macy 	if (ZSTD_isError(c_len)) {
405eda14cbcSMatt Macy 		/*
406eda14cbcSMatt Macy 		 * If we are aborting the compression because the saves are
407eda14cbcSMatt Macy 		 * too small, that is not a failure. Everything else is a
408eda14cbcSMatt Macy 		 * failure, so increment the compression failure counter.
409eda14cbcSMatt Macy 		 */
410eda14cbcSMatt Macy 		if (ZSTD_getErrorCode(c_len) != ZSTD_error_dstSize_tooSmall) {
411eda14cbcSMatt Macy 			ZSTDSTAT_BUMP(zstd_stat_com_fail);
412eda14cbcSMatt Macy 		}
413eda14cbcSMatt Macy 		return (s_len);
414eda14cbcSMatt Macy 	}
415eda14cbcSMatt Macy 
416eda14cbcSMatt Macy 	/*
417eda14cbcSMatt Macy 	 * Encode the compressed buffer size at the start. We'll need this in
418eda14cbcSMatt Macy 	 * decompression to counter the effects of padding which might be added
419eda14cbcSMatt Macy 	 * to the compressed buffer and which, if unhandled, would confuse the
420eda14cbcSMatt Macy 	 * hell out of our decompression function.
421eda14cbcSMatt Macy 	 */
422eda14cbcSMatt Macy 	hdr->c_len = BE_32(c_len);
423eda14cbcSMatt Macy 
424eda14cbcSMatt Macy 	/*
425eda14cbcSMatt Macy 	 * Check version for overflow.
426eda14cbcSMatt Macy 	 * The limit of 24 bits must not be exceeded. This allows a maximum
427eda14cbcSMatt Macy 	 * version 1677.72.15 which we don't expect to be ever reached.
428eda14cbcSMatt Macy 	 */
429eda14cbcSMatt Macy 	ASSERT3U(ZSTD_VERSION_NUMBER, <=, 0xFFFFFF);
430eda14cbcSMatt Macy 
431eda14cbcSMatt Macy 	/*
432eda14cbcSMatt Macy 	 * Encode the compression level as well. We may need to know the
433eda14cbcSMatt Macy 	 * original compression level if compressed_arc is disabled, to match
434eda14cbcSMatt Macy 	 * the compression settings to write this block to the L2ARC.
435eda14cbcSMatt Macy 	 *
436eda14cbcSMatt Macy 	 * Encode the actual level, so if the enum changes in the future, we
437eda14cbcSMatt Macy 	 * will be compatible.
438eda14cbcSMatt Macy 	 *
439eda14cbcSMatt Macy 	 * The upper 24 bits store the ZSTD version to be able to provide
440eda14cbcSMatt Macy 	 * future compatibility, since new versions might enhance the
441eda14cbcSMatt Macy 	 * compression algorithm in a way, where the compressed data will
442eda14cbcSMatt Macy 	 * change.
443eda14cbcSMatt Macy 	 *
444eda14cbcSMatt Macy 	 * As soon as such incompatibility occurs, handling code needs to be
445eda14cbcSMatt Macy 	 * added, differentiating between the versions.
446eda14cbcSMatt Macy 	 */
447eda14cbcSMatt Macy 	hdr->version = ZSTD_VERSION_NUMBER;
448eda14cbcSMatt Macy 	hdr->level = level;
449eda14cbcSMatt Macy 	hdr->raw_version_level = BE_32(hdr->raw_version_level);
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 	return (c_len + sizeof (*hdr));
452eda14cbcSMatt Macy }
453eda14cbcSMatt Macy 
454eda14cbcSMatt Macy /* Decompress block using zstd and return its stored level */
455eda14cbcSMatt Macy int
456eda14cbcSMatt Macy zfs_zstd_decompress_level(void *s_start, void *d_start, size_t s_len,
457eda14cbcSMatt Macy     size_t d_len, uint8_t *level)
458eda14cbcSMatt Macy {
459eda14cbcSMatt Macy 	ZSTD_DCtx *dctx;
460eda14cbcSMatt Macy 	size_t result;
461eda14cbcSMatt Macy 	int16_t zstd_level;
462eda14cbcSMatt Macy 	uint32_t c_len;
463eda14cbcSMatt Macy 	const zfs_zstdhdr_t *hdr;
464eda14cbcSMatt Macy 	zfs_zstdhdr_t hdr_copy;
465eda14cbcSMatt Macy 
466eda14cbcSMatt Macy 	hdr = (const zfs_zstdhdr_t *)s_start;
467eda14cbcSMatt Macy 	c_len = BE_32(hdr->c_len);
468eda14cbcSMatt Macy 
469eda14cbcSMatt Macy 	/*
470eda14cbcSMatt Macy 	 * Make a copy instead of directly converting the header, since we must
471eda14cbcSMatt Macy 	 * not modify the original data that may be used again later.
472eda14cbcSMatt Macy 	 */
473eda14cbcSMatt Macy 	hdr_copy.raw_version_level = BE_32(hdr->raw_version_level);
474eda14cbcSMatt Macy 
475eda14cbcSMatt Macy 	/*
476eda14cbcSMatt Macy 	 * NOTE: We ignore the ZSTD version for now. As soon as any
477eda14cbcSMatt Macy 	 * incompatibility occurrs, it has to be handled accordingly.
478eda14cbcSMatt Macy 	 * The version can be accessed via `hdr_copy.version`.
479eda14cbcSMatt Macy 	 */
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 	/*
482eda14cbcSMatt Macy 	 * Convert and check the level
483eda14cbcSMatt Macy 	 * An invalid level is a strong indicator for data corruption! In such
484eda14cbcSMatt Macy 	 * case return an error so the upper layers can try to fix it.
485eda14cbcSMatt Macy 	 */
486eda14cbcSMatt Macy 	if (zstd_enum_to_level(hdr_copy.level, &zstd_level)) {
487eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_dec_inval);
488eda14cbcSMatt Macy 		return (1);
489eda14cbcSMatt Macy 	}
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy 	ASSERT3U(d_len, >=, s_len);
492eda14cbcSMatt Macy 	ASSERT3U(hdr_copy.level, !=, ZIO_COMPLEVEL_INHERIT);
493eda14cbcSMatt Macy 
494eda14cbcSMatt Macy 	/* Invalid compressed buffer size encoded at start */
495eda14cbcSMatt Macy 	if (c_len + sizeof (*hdr) > s_len) {
496eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_dec_header_inval);
497eda14cbcSMatt Macy 		return (1);
498eda14cbcSMatt Macy 	}
499eda14cbcSMatt Macy 
500eda14cbcSMatt Macy 	dctx = ZSTD_createDCtx_advanced(zstd_dctx_malloc);
501eda14cbcSMatt Macy 	if (!dctx) {
502eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_dec_alloc_fail);
503eda14cbcSMatt Macy 		return (1);
504eda14cbcSMatt Macy 	}
505eda14cbcSMatt Macy 
506eda14cbcSMatt Macy 	/* Set header type to "magicless" */
507eda14cbcSMatt Macy 	ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, ZSTD_f_zstd1_magicless);
508eda14cbcSMatt Macy 
509eda14cbcSMatt Macy 	/* Decompress the data and release the context */
510eda14cbcSMatt Macy 	result = ZSTD_decompressDCtx(dctx, d_start, d_len, hdr->data, c_len);
511eda14cbcSMatt Macy 	ZSTD_freeDCtx(dctx);
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 	/*
514eda14cbcSMatt Macy 	 * Returns 0 on success (decompression function returned non-negative)
515eda14cbcSMatt Macy 	 * and non-zero on failure (decompression function returned negative.
516eda14cbcSMatt Macy 	 */
517eda14cbcSMatt Macy 	if (ZSTD_isError(result)) {
518eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_dec_fail);
519eda14cbcSMatt Macy 		return (1);
520eda14cbcSMatt Macy 	}
521eda14cbcSMatt Macy 
522eda14cbcSMatt Macy 	if (level) {
523eda14cbcSMatt Macy 		*level = hdr_copy.level;
524eda14cbcSMatt Macy 	}
525eda14cbcSMatt Macy 
526eda14cbcSMatt Macy 	return (0);
527eda14cbcSMatt Macy }
528eda14cbcSMatt Macy 
529eda14cbcSMatt Macy /* Decompress datablock using zstd */
530eda14cbcSMatt Macy int
531eda14cbcSMatt Macy zfs_zstd_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len,
532eda14cbcSMatt Macy     int level __maybe_unused)
533eda14cbcSMatt Macy {
534eda14cbcSMatt Macy 
535eda14cbcSMatt Macy 	return (zfs_zstd_decompress_level(s_start, d_start, s_len, d_len,
536eda14cbcSMatt Macy 	    NULL));
537eda14cbcSMatt Macy }
538eda14cbcSMatt Macy 
539eda14cbcSMatt Macy /* Allocator for zstd compression context using mempool_allocator */
540eda14cbcSMatt Macy static void *
541eda14cbcSMatt Macy zstd_alloc(void *opaque __maybe_unused, size_t size)
542eda14cbcSMatt Macy {
543eda14cbcSMatt Macy 	size_t nbytes = sizeof (struct zstd_kmem) + size;
544eda14cbcSMatt Macy 	struct zstd_kmem *z = NULL;
545eda14cbcSMatt Macy 
546eda14cbcSMatt Macy 	z = (struct zstd_kmem *)zstd_mempool_alloc(zstd_mempool_cctx, nbytes);
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy 	if (!z) {
549eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_alloc_fail);
550eda14cbcSMatt Macy 		return (NULL);
551eda14cbcSMatt Macy 	}
552eda14cbcSMatt Macy 
553eda14cbcSMatt Macy 	return ((void*)z + (sizeof (struct zstd_kmem)));
554eda14cbcSMatt Macy }
555eda14cbcSMatt Macy 
556eda14cbcSMatt Macy /*
557eda14cbcSMatt Macy  * Allocator for zstd decompression context using mempool_allocator with
558eda14cbcSMatt Macy  * fallback to reserved memory if allocation fails
559eda14cbcSMatt Macy  */
560eda14cbcSMatt Macy static void *
561eda14cbcSMatt Macy zstd_dctx_alloc(void *opaque __maybe_unused, size_t size)
562eda14cbcSMatt Macy {
563eda14cbcSMatt Macy 	size_t nbytes = sizeof (struct zstd_kmem) + size;
564eda14cbcSMatt Macy 	struct zstd_kmem *z = NULL;
565eda14cbcSMatt Macy 	enum zstd_kmem_type type = ZSTD_KMEM_DEFAULT;
566eda14cbcSMatt Macy 
567eda14cbcSMatt Macy 	z = (struct zstd_kmem *)zstd_mempool_alloc(zstd_mempool_dctx, nbytes);
568eda14cbcSMatt Macy 	if (!z) {
569eda14cbcSMatt Macy 		/* Try harder, decompression shall not fail */
570eda14cbcSMatt Macy 		z = vmem_alloc(nbytes, KM_SLEEP);
571eda14cbcSMatt Macy 		if (z) {
572eda14cbcSMatt Macy 			z->pool = NULL;
573eda14cbcSMatt Macy 		}
574eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_alloc_fail);
575eda14cbcSMatt Macy 	} else {
576eda14cbcSMatt Macy 		return ((void*)z + (sizeof (struct zstd_kmem)));
577eda14cbcSMatt Macy 	}
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 	/* Fallback if everything fails */
580eda14cbcSMatt Macy 	if (!z) {
581eda14cbcSMatt Macy 		/*
582eda14cbcSMatt Macy 		 * Barrier since we only can handle it in a single thread. All
583eda14cbcSMatt Macy 		 * other following threads need to wait here until decompression
584eda14cbcSMatt Macy 		 * is completed. zstd_free will release this barrier later.
585eda14cbcSMatt Macy 		 */
586eda14cbcSMatt Macy 		mutex_enter(&zstd_dctx_fallback.barrier);
587eda14cbcSMatt Macy 
588eda14cbcSMatt Macy 		z = zstd_dctx_fallback.mem;
589eda14cbcSMatt Macy 		type = ZSTD_KMEM_DCTX;
590eda14cbcSMatt Macy 		ZSTDSTAT_BUMP(zstd_stat_alloc_fallback);
591eda14cbcSMatt Macy 	}
592eda14cbcSMatt Macy 
593eda14cbcSMatt Macy 	/* Allocation should always be successful */
594eda14cbcSMatt Macy 	if (!z) {
595eda14cbcSMatt Macy 		return (NULL);
596eda14cbcSMatt Macy 	}
597eda14cbcSMatt Macy 
598eda14cbcSMatt Macy 	z->kmem_type = type;
599eda14cbcSMatt Macy 	z->kmem_size = nbytes;
600eda14cbcSMatt Macy 
601eda14cbcSMatt Macy 	return ((void*)z + (sizeof (struct zstd_kmem)));
602eda14cbcSMatt Macy }
603eda14cbcSMatt Macy 
604eda14cbcSMatt Macy /* Free allocated memory by its specific type */
605eda14cbcSMatt Macy static void
606eda14cbcSMatt Macy zstd_free(void *opaque __maybe_unused, void *ptr)
607eda14cbcSMatt Macy {
608eda14cbcSMatt Macy 	struct zstd_kmem *z = (ptr - sizeof (struct zstd_kmem));
609eda14cbcSMatt Macy 	enum zstd_kmem_type type;
610eda14cbcSMatt Macy 
611eda14cbcSMatt Macy 	ASSERT3U(z->kmem_type, <, ZSTD_KMEM_COUNT);
612eda14cbcSMatt Macy 	ASSERT3U(z->kmem_type, >, ZSTD_KMEM_UNKNOWN);
613eda14cbcSMatt Macy 
614eda14cbcSMatt Macy 	type = z->kmem_type;
615eda14cbcSMatt Macy 	switch (type) {
616eda14cbcSMatt Macy 	case ZSTD_KMEM_DEFAULT:
617eda14cbcSMatt Macy 		vmem_free(z, z->kmem_size);
618eda14cbcSMatt Macy 		break;
619eda14cbcSMatt Macy 	case ZSTD_KMEM_POOL:
620eda14cbcSMatt Macy 		zstd_mempool_free(z);
621eda14cbcSMatt Macy 		break;
622eda14cbcSMatt Macy 	case ZSTD_KMEM_DCTX:
623eda14cbcSMatt Macy 		mutex_exit(&zstd_dctx_fallback.barrier);
624eda14cbcSMatt Macy 		break;
625eda14cbcSMatt Macy 	default:
626eda14cbcSMatt Macy 		break;
627eda14cbcSMatt Macy 	}
628eda14cbcSMatt Macy }
629eda14cbcSMatt Macy 
630eda14cbcSMatt Macy /* Allocate fallback memory to ensure safe decompression */
631eda14cbcSMatt Macy static void __init
632eda14cbcSMatt Macy create_fallback_mem(struct zstd_fallback_mem *mem, size_t size)
633eda14cbcSMatt Macy {
634eda14cbcSMatt Macy 	mem->mem_size = size;
635eda14cbcSMatt Macy 	mem->mem = vmem_zalloc(mem->mem_size, KM_SLEEP);
636eda14cbcSMatt Macy 	mutex_init(&mem->barrier, NULL, MUTEX_DEFAULT, NULL);
637eda14cbcSMatt Macy }
638eda14cbcSMatt Macy 
639eda14cbcSMatt Macy /* Initialize memory pool barrier mutexes */
640eda14cbcSMatt Macy static void __init
641eda14cbcSMatt Macy zstd_mempool_init(void)
642eda14cbcSMatt Macy {
643eda14cbcSMatt Macy 	zstd_mempool_cctx = (struct zstd_pool *)
644eda14cbcSMatt Macy 	    kmem_zalloc(ZSTD_POOL_MAX * sizeof (struct zstd_pool), KM_SLEEP);
645eda14cbcSMatt Macy 	zstd_mempool_dctx = (struct zstd_pool *)
646eda14cbcSMatt Macy 	    kmem_zalloc(ZSTD_POOL_MAX * sizeof (struct zstd_pool), KM_SLEEP);
647eda14cbcSMatt Macy 
648eda14cbcSMatt Macy 	for (int i = 0; i < ZSTD_POOL_MAX; i++) {
649eda14cbcSMatt Macy 		mutex_init(&zstd_mempool_cctx[i].barrier, NULL,
650eda14cbcSMatt Macy 		    MUTEX_DEFAULT, NULL);
651eda14cbcSMatt Macy 		mutex_init(&zstd_mempool_dctx[i].barrier, NULL,
652eda14cbcSMatt Macy 		    MUTEX_DEFAULT, NULL);
653eda14cbcSMatt Macy 	}
654eda14cbcSMatt Macy }
655eda14cbcSMatt Macy 
656eda14cbcSMatt Macy /* Initialize zstd-related memory handling */
657eda14cbcSMatt Macy static int __init
658eda14cbcSMatt Macy zstd_meminit(void)
659eda14cbcSMatt Macy {
660eda14cbcSMatt Macy 	zstd_mempool_init();
661eda14cbcSMatt Macy 
662eda14cbcSMatt Macy 	/*
663eda14cbcSMatt Macy 	 * Estimate the size of the fallback decompression context.
664eda14cbcSMatt Macy 	 * The expected size on x64 with current ZSTD should be about 160 KB.
665eda14cbcSMatt Macy 	 */
666eda14cbcSMatt Macy 	create_fallback_mem(&zstd_dctx_fallback,
667eda14cbcSMatt Macy 	    P2ROUNDUP(ZSTD_estimateDCtxSize() + sizeof (struct zstd_kmem),
668eda14cbcSMatt Macy 	    PAGESIZE));
669eda14cbcSMatt Macy 
670eda14cbcSMatt Macy 	return (0);
671eda14cbcSMatt Macy }
672eda14cbcSMatt Macy 
673eda14cbcSMatt Macy /* Release object from pool and free memory */
674eda14cbcSMatt Macy static void __exit
675eda14cbcSMatt Macy release_pool(struct zstd_pool *pool)
676eda14cbcSMatt Macy {
677eda14cbcSMatt Macy 	mutex_destroy(&pool->barrier);
678eda14cbcSMatt Macy 	vmem_free(pool->mem, pool->size);
679eda14cbcSMatt Macy 	pool->mem = NULL;
680eda14cbcSMatt Macy 	pool->size = 0;
681eda14cbcSMatt Macy }
682eda14cbcSMatt Macy 
683eda14cbcSMatt Macy /* Release memory pool objects */
684eda14cbcSMatt Macy static void __exit
685eda14cbcSMatt Macy zstd_mempool_deinit(void)
686eda14cbcSMatt Macy {
687eda14cbcSMatt Macy 	for (int i = 0; i < ZSTD_POOL_MAX; i++) {
688eda14cbcSMatt Macy 		release_pool(&zstd_mempool_cctx[i]);
689eda14cbcSMatt Macy 		release_pool(&zstd_mempool_dctx[i]);
690eda14cbcSMatt Macy 	}
691eda14cbcSMatt Macy 
692eda14cbcSMatt Macy 	kmem_free(zstd_mempool_dctx, ZSTD_POOL_MAX * sizeof (struct zstd_pool));
693eda14cbcSMatt Macy 	kmem_free(zstd_mempool_cctx, ZSTD_POOL_MAX * sizeof (struct zstd_pool));
694eda14cbcSMatt Macy 	zstd_mempool_dctx = NULL;
695eda14cbcSMatt Macy 	zstd_mempool_cctx = NULL;
696eda14cbcSMatt Macy }
697eda14cbcSMatt Macy 
698c40487d4SMatt Macy /* release unused memory from pool */
699c40487d4SMatt Macy 
700c40487d4SMatt Macy void
701c40487d4SMatt Macy zfs_zstd_cache_reap_now(void)
702c40487d4SMatt Macy {
703*36639c39SMateusz Guzik 
704*36639c39SMateusz Guzik 	/*
705*36639c39SMateusz Guzik 	 * Short-circuit if there are no buffers to begin with.
706*36639c39SMateusz Guzik 	 */
707*36639c39SMateusz Guzik 	if (ZSTDSTAT(zstd_stat_buffers) == 0)
708*36639c39SMateusz Guzik 		return;
709*36639c39SMateusz Guzik 
710c40487d4SMatt Macy 	/*
711c40487d4SMatt Macy 	 * calling alloc with zero size seeks
712c40487d4SMatt Macy 	 * and releases old unused objects
713c40487d4SMatt Macy 	 */
714c40487d4SMatt Macy 	zstd_mempool_alloc(zstd_mempool_cctx, 0);
715c40487d4SMatt Macy 	zstd_mempool_alloc(zstd_mempool_dctx, 0);
716c40487d4SMatt Macy }
717c40487d4SMatt Macy 
718eda14cbcSMatt Macy extern int __init
719eda14cbcSMatt Macy zstd_init(void)
720eda14cbcSMatt Macy {
721eda14cbcSMatt Macy 	/* Set pool size by using maximum sane thread count * 4 */
722eda14cbcSMatt Macy 	pool_count = (boot_ncpus * 4);
723eda14cbcSMatt Macy 	zstd_meminit();
724eda14cbcSMatt Macy 
725eda14cbcSMatt Macy 	/* Initialize kstat */
726eda14cbcSMatt Macy 	zstd_ksp = kstat_create("zfs", 0, "zstd", "misc",
727eda14cbcSMatt Macy 	    KSTAT_TYPE_NAMED, sizeof (zstd_stats) / sizeof (kstat_named_t),
728eda14cbcSMatt Macy 	    KSTAT_FLAG_VIRTUAL);
729eda14cbcSMatt Macy 	if (zstd_ksp != NULL) {
730eda14cbcSMatt Macy 		zstd_ksp->ks_data = &zstd_stats;
731eda14cbcSMatt Macy 		kstat_install(zstd_ksp);
732eda14cbcSMatt Macy 	}
733eda14cbcSMatt Macy 
734eda14cbcSMatt Macy 	return (0);
735eda14cbcSMatt Macy }
736eda14cbcSMatt Macy 
737eda14cbcSMatt Macy extern void __exit
738eda14cbcSMatt Macy zstd_fini(void)
739eda14cbcSMatt Macy {
740eda14cbcSMatt Macy 	/* Deinitialize kstat */
741eda14cbcSMatt Macy 	if (zstd_ksp != NULL) {
742eda14cbcSMatt Macy 		kstat_delete(zstd_ksp);
743eda14cbcSMatt Macy 		zstd_ksp = NULL;
744eda14cbcSMatt Macy 	}
745eda14cbcSMatt Macy 
746eda14cbcSMatt Macy 	/* Release fallback memory */
747eda14cbcSMatt Macy 	vmem_free(zstd_dctx_fallback.mem, zstd_dctx_fallback.mem_size);
748eda14cbcSMatt Macy 	mutex_destroy(&zstd_dctx_fallback.barrier);
749eda14cbcSMatt Macy 
750eda14cbcSMatt Macy 	/* Deinit memory pool */
751eda14cbcSMatt Macy 	zstd_mempool_deinit();
752eda14cbcSMatt Macy }
753eda14cbcSMatt Macy 
754eda14cbcSMatt Macy #if defined(_KERNEL)
755eda14cbcSMatt Macy module_init(zstd_init);
756eda14cbcSMatt Macy module_exit(zstd_fini);
757eda14cbcSMatt Macy 
758eda14cbcSMatt Macy ZFS_MODULE_DESCRIPTION("ZSTD Compression for ZFS");
759c40487d4SMatt Macy ZFS_MODULE_LICENSE("Dual BSD/GPL");
760eda14cbcSMatt Macy ZFS_MODULE_VERSION(ZSTD_VERSION_STRING);
761eda14cbcSMatt Macy 
762eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_zstd_compress);
763eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_zstd_decompress_level);
764eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_zstd_decompress);
765c40487d4SMatt Macy EXPORT_SYMBOL(zfs_zstd_cache_reap_now);
766eda14cbcSMatt Macy #endif
767