1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016-present, Facebook, Inc.
9 #include <linux/bitmap.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
14 #include <linux/sched/mm.h>
15 #include <linux/pagemap.h>
16 #include <linux/refcount.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/zstd.h>
22 #include "compression.h"
25 #define ZSTD_BTRFS_MAX_WINDOWLOG 17
26 #define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
27 #define ZSTD_BTRFS_DEFAULT_LEVEL 3
28 #define ZSTD_BTRFS_MAX_LEVEL 15
29 /* 307s to avoid pathologically clashing with transaction commit */
30 #define ZSTD_BTRFS_RECLAIM_JIFFIES (307 * HZ)
32 static zstd_parameters zstd_get_btrfs_parameters(unsigned int level,
35 zstd_parameters params = zstd_get_params(level, src_len);
37 if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG)
38 params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG;
39 WARN_ON(src_len > ZSTD_BTRFS_MAX_INPUT);
48 unsigned int req_level;
49 unsigned long last_used; /* jiffies */
50 struct list_head list;
51 struct list_head lru_list;
52 zstd_in_buffer in_buf;
53 zstd_out_buffer out_buf;
57 * Zstd Workspace Management
59 * Zstd workspaces have different memory requirements depending on the level.
60 * The zstd workspaces are managed by having individual lists for each level
61 * and a global lru. Forward progress is maintained by protecting a max level
64 * Getting a workspace is done by using the bitmap to identify the levels that
65 * have available workspaces and scans up. This lets us recycle higher level
66 * workspaces because of the monotonic memory guarantee. A workspace's
67 * last_used is only updated if it is being used by the corresponding memory
68 * level. Putting a workspace involves adding it back to the appropriate places
69 * and adding it back to the lru if necessary.
71 * A timer is used to reclaim workspaces if they have not been used for
72 * ZSTD_BTRFS_RECLAIM_JIFFIES. This helps keep only active workspaces around.
73 * The upper bound is provided by the workqueue limit which is 2 (percpu limit).
76 struct zstd_workspace_manager {
77 const struct btrfs_compress_op *ops;
79 struct list_head lru_list;
80 struct list_head idle_ws[ZSTD_BTRFS_MAX_LEVEL];
81 unsigned long active_map;
82 wait_queue_head_t wait;
83 struct timer_list timer;
86 static struct zstd_workspace_manager wsm;
88 static size_t zstd_ws_mem_sizes[ZSTD_BTRFS_MAX_LEVEL];
90 static inline struct workspace *list_to_workspace(struct list_head *list)
92 return container_of(list, struct workspace, list);
95 void zstd_free_workspace(struct list_head *ws);
96 struct list_head *zstd_alloc_workspace(unsigned int level);
99 * Timer callback to free unused workspaces.
103 * This scans the lru_list and attempts to reclaim any workspace that hasn't
104 * been used for ZSTD_BTRFS_RECLAIM_JIFFIES.
106 * The context is softirq and does not need the _bh locking primitives.
108 static void zstd_reclaim_timer_fn(struct timer_list *timer)
110 unsigned long reclaim_threshold = jiffies - ZSTD_BTRFS_RECLAIM_JIFFIES;
111 struct list_head *pos, *next;
113 spin_lock(&wsm.lock);
115 if (list_empty(&wsm.lru_list)) {
116 spin_unlock(&wsm.lock);
120 list_for_each_prev_safe(pos, next, &wsm.lru_list) {
121 struct workspace *victim = container_of(pos, struct workspace,
125 if (time_after(victim->last_used, reclaim_threshold))
128 /* workspace is in use */
129 if (victim->req_level)
132 level = victim->level;
133 list_del(&victim->lru_list);
134 list_del(&victim->list);
135 zstd_free_workspace(&victim->list);
137 if (list_empty(&wsm.idle_ws[level - 1]))
138 clear_bit(level - 1, &wsm.active_map);
142 if (!list_empty(&wsm.lru_list))
143 mod_timer(&wsm.timer, jiffies + ZSTD_BTRFS_RECLAIM_JIFFIES);
145 spin_unlock(&wsm.lock);
149 * Calculate monotonic memory bounds.
151 * It is possible based on the level configurations that a higher level
152 * workspace uses less memory than a lower level workspace. In order to reuse
153 * workspaces, this must be made a monotonic relationship. This precomputes
154 * the required memory for each level and enforces the monotonicity between
155 * level and memory required.
157 static void zstd_calc_ws_mem_sizes(void)
162 for (level = 1; level <= ZSTD_BTRFS_MAX_LEVEL; level++) {
163 zstd_parameters params =
164 zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT);
167 zstd_cstream_workspace_bound(¶ms.cParams),
168 zstd_dstream_workspace_bound(ZSTD_BTRFS_MAX_INPUT));
170 max_size = max_t(size_t, max_size, level_size);
171 zstd_ws_mem_sizes[level - 1] = max_size;
175 void zstd_init_workspace_manager(void)
177 struct list_head *ws;
180 zstd_calc_ws_mem_sizes();
182 wsm.ops = &btrfs_zstd_compress;
183 spin_lock_init(&wsm.lock);
184 init_waitqueue_head(&wsm.wait);
185 timer_setup(&wsm.timer, zstd_reclaim_timer_fn, 0);
187 INIT_LIST_HEAD(&wsm.lru_list);
188 for (i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++)
189 INIT_LIST_HEAD(&wsm.idle_ws[i]);
191 ws = zstd_alloc_workspace(ZSTD_BTRFS_MAX_LEVEL);
194 "BTRFS: cannot preallocate zstd compression workspace\n");
196 set_bit(ZSTD_BTRFS_MAX_LEVEL - 1, &wsm.active_map);
197 list_add(ws, &wsm.idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1]);
201 void zstd_cleanup_workspace_manager(void)
203 struct workspace *workspace;
206 spin_lock_bh(&wsm.lock);
207 for (i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++) {
208 while (!list_empty(&wsm.idle_ws[i])) {
209 workspace = container_of(wsm.idle_ws[i].next,
210 struct workspace, list);
211 list_del(&workspace->list);
212 list_del(&workspace->lru_list);
213 zstd_free_workspace(&workspace->list);
216 spin_unlock_bh(&wsm.lock);
218 del_timer_sync(&wsm.timer);
222 * Find workspace for given level.
224 * @level: compression level
226 * This iterates over the set bits in the active_map beginning at the requested
227 * compression level. This lets us utilize already allocated workspaces before
228 * allocating a new one. If the workspace is of a larger size, it is used, but
229 * the place in the lru_list and last_used times are not updated. This is to
230 * offer the opportunity to reclaim the workspace in favor of allocating an
231 * appropriately sized one in the future.
233 static struct list_head *zstd_find_workspace(unsigned int level)
235 struct list_head *ws;
236 struct workspace *workspace;
239 spin_lock_bh(&wsm.lock);
240 for_each_set_bit_from(i, &wsm.active_map, ZSTD_BTRFS_MAX_LEVEL) {
241 if (!list_empty(&wsm.idle_ws[i])) {
242 ws = wsm.idle_ws[i].next;
243 workspace = list_to_workspace(ws);
245 /* keep its place if it's a lower level using this */
246 workspace->req_level = level;
247 if (level == workspace->level)
248 list_del(&workspace->lru_list);
249 if (list_empty(&wsm.idle_ws[i]))
250 clear_bit(i, &wsm.active_map);
251 spin_unlock_bh(&wsm.lock);
255 spin_unlock_bh(&wsm.lock);
261 * Zstd get_workspace for level.
263 * @level: compression level
265 * If @level is 0, then any compression level can be used. Therefore, we begin
266 * scanning from 1. We first scan through possible workspaces and then after
267 * attempt to allocate a new workspace. If we fail to allocate one due to
268 * memory pressure, go to sleep waiting for the max level workspace to free up.
270 struct list_head *zstd_get_workspace(unsigned int level)
272 struct list_head *ws;
273 unsigned int nofs_flag;
275 /* level == 0 means we can use any workspace */
280 ws = zstd_find_workspace(level);
284 nofs_flag = memalloc_nofs_save();
285 ws = zstd_alloc_workspace(level);
286 memalloc_nofs_restore(nofs_flag);
291 prepare_to_wait(&wsm.wait, &wait, TASK_UNINTERRUPTIBLE);
293 finish_wait(&wsm.wait, &wait);
302 * Zstd put_workspace.
304 * @ws: list_head for the workspace
306 * When putting back a workspace, we only need to update the LRU if we are of
307 * the requested compression level. Here is where we continue to protect the
308 * max level workspace or update last_used accordingly. If the reclaim timer
309 * isn't set, it is also set here. Only the max level workspace tries and wakes
310 * up waiting workspaces.
312 void zstd_put_workspace(struct list_head *ws)
314 struct workspace *workspace = list_to_workspace(ws);
316 spin_lock_bh(&wsm.lock);
318 /* A node is only taken off the lru if we are the corresponding level */
319 if (workspace->req_level == workspace->level) {
320 /* Hide a max level workspace from reclaim */
321 if (list_empty(&wsm.idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1])) {
322 INIT_LIST_HEAD(&workspace->lru_list);
324 workspace->last_used = jiffies;
325 list_add(&workspace->lru_list, &wsm.lru_list);
326 if (!timer_pending(&wsm.timer))
327 mod_timer(&wsm.timer,
328 jiffies + ZSTD_BTRFS_RECLAIM_JIFFIES);
332 set_bit(workspace->level - 1, &wsm.active_map);
333 list_add(&workspace->list, &wsm.idle_ws[workspace->level - 1]);
334 workspace->req_level = 0;
336 spin_unlock_bh(&wsm.lock);
338 if (workspace->level == ZSTD_BTRFS_MAX_LEVEL)
339 cond_wake_up(&wsm.wait);
342 void zstd_free_workspace(struct list_head *ws)
344 struct workspace *workspace = list_entry(ws, struct workspace, list);
346 kvfree(workspace->mem);
347 kfree(workspace->buf);
351 struct list_head *zstd_alloc_workspace(unsigned int level)
353 struct workspace *workspace;
355 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
357 return ERR_PTR(-ENOMEM);
359 workspace->size = zstd_ws_mem_sizes[level - 1];
360 workspace->level = level;
361 workspace->req_level = level;
362 workspace->last_used = jiffies;
363 workspace->mem = kvmalloc(workspace->size, GFP_KERNEL | __GFP_NOWARN);
364 workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
365 if (!workspace->mem || !workspace->buf)
368 INIT_LIST_HEAD(&workspace->list);
369 INIT_LIST_HEAD(&workspace->lru_list);
371 return &workspace->list;
373 zstd_free_workspace(&workspace->list);
374 return ERR_PTR(-ENOMEM);
377 int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
378 u64 start, struct page **pages, unsigned long *out_pages,
379 unsigned long *total_in, unsigned long *total_out)
381 struct workspace *workspace = list_entry(ws, struct workspace, list);
382 zstd_cstream *stream;
385 struct page *in_page = NULL; /* The current page to read */
386 struct page *out_page = NULL; /* The current page to write to */
387 unsigned long tot_in = 0;
388 unsigned long tot_out = 0;
389 unsigned long len = *total_out;
390 const unsigned long nr_dest_pages = *out_pages;
391 unsigned long max_out = nr_dest_pages * PAGE_SIZE;
392 zstd_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
399 /* Initialize the stream */
400 stream = zstd_init_cstream(¶ms, len, workspace->mem,
403 pr_warn("BTRFS: zstd_init_cstream failed\n");
408 /* map in the first page of input data */
409 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
410 workspace->in_buf.src = kmap_local_page(in_page);
411 workspace->in_buf.pos = 0;
412 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
414 /* Allocate and map in the output buffer */
415 out_page = btrfs_alloc_compr_page();
416 if (out_page == NULL) {
420 pages[nr_pages++] = out_page;
421 workspace->out_buf.dst = page_address(out_page);
422 workspace->out_buf.pos = 0;
423 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
428 ret2 = zstd_compress_stream(stream, &workspace->out_buf,
430 if (zstd_is_error(ret2)) {
431 pr_debug("BTRFS: zstd_compress_stream returned %d\n",
432 zstd_get_error_code(ret2));
437 /* Check to see if we are making it bigger */
438 if (tot_in + workspace->in_buf.pos > 8192 &&
439 tot_in + workspace->in_buf.pos <
440 tot_out + workspace->out_buf.pos) {
445 /* We've reached the end of our output range */
446 if (workspace->out_buf.pos >= max_out) {
447 tot_out += workspace->out_buf.pos;
452 /* Check if we need more output space */
453 if (workspace->out_buf.pos == workspace->out_buf.size) {
454 tot_out += PAGE_SIZE;
455 max_out -= PAGE_SIZE;
456 if (nr_pages == nr_dest_pages) {
460 out_page = btrfs_alloc_compr_page();
461 if (out_page == NULL) {
465 pages[nr_pages++] = out_page;
466 workspace->out_buf.dst = page_address(out_page);
467 workspace->out_buf.pos = 0;
468 workspace->out_buf.size = min_t(size_t, max_out,
472 /* We've reached the end of the input */
473 if (workspace->in_buf.pos >= len) {
474 tot_in += workspace->in_buf.pos;
478 /* Check if we need more input */
479 if (workspace->in_buf.pos == workspace->in_buf.size) {
481 kunmap_local(workspace->in_buf.src);
485 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
486 workspace->in_buf.src = kmap_local_page(in_page);
487 workspace->in_buf.pos = 0;
488 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
494 ret2 = zstd_end_stream(stream, &workspace->out_buf);
495 if (zstd_is_error(ret2)) {
496 pr_debug("BTRFS: zstd_end_stream returned %d\n",
497 zstd_get_error_code(ret2));
502 tot_out += workspace->out_buf.pos;
505 if (workspace->out_buf.pos >= max_out) {
506 tot_out += workspace->out_buf.pos;
511 tot_out += PAGE_SIZE;
512 max_out -= PAGE_SIZE;
513 if (nr_pages == nr_dest_pages) {
517 out_page = btrfs_alloc_compr_page();
518 if (out_page == NULL) {
522 pages[nr_pages++] = out_page;
523 workspace->out_buf.dst = page_address(out_page);
524 workspace->out_buf.pos = 0;
525 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
528 if (tot_out >= tot_in) {
535 *total_out = tot_out;
537 *out_pages = nr_pages;
538 if (workspace->in_buf.src) {
539 kunmap_local(workspace->in_buf.src);
545 int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
547 struct workspace *workspace = list_entry(ws, struct workspace, list);
548 struct page **pages_in = cb->compressed_pages;
549 size_t srclen = cb->compressed_len;
550 zstd_dstream *stream;
552 unsigned long page_in_index = 0;
553 unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
554 unsigned long buf_start;
555 unsigned long total_out = 0;
557 stream = zstd_init_dstream(
558 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
560 pr_debug("BTRFS: zstd_init_dstream failed\n");
565 workspace->in_buf.src = kmap_local_page(pages_in[page_in_index]);
566 workspace->in_buf.pos = 0;
567 workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
569 workspace->out_buf.dst = workspace->buf;
570 workspace->out_buf.pos = 0;
571 workspace->out_buf.size = PAGE_SIZE;
576 ret2 = zstd_decompress_stream(stream, &workspace->out_buf,
578 if (zstd_is_error(ret2)) {
579 pr_debug("BTRFS: zstd_decompress_stream returned %d\n",
580 zstd_get_error_code(ret2));
584 buf_start = total_out;
585 total_out += workspace->out_buf.pos;
586 workspace->out_buf.pos = 0;
588 ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
589 total_out - buf_start, cb, buf_start);
593 if (workspace->in_buf.pos >= srclen)
596 /* Check if we've hit the end of a frame */
600 if (workspace->in_buf.pos == workspace->in_buf.size) {
601 kunmap_local(workspace->in_buf.src);
603 if (page_in_index >= total_pages_in) {
604 workspace->in_buf.src = NULL;
609 workspace->in_buf.src = kmap_local_page(pages_in[page_in_index]);
610 workspace->in_buf.pos = 0;
611 workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
616 if (workspace->in_buf.src)
617 kunmap_local(workspace->in_buf.src);
621 int zstd_decompress(struct list_head *ws, const u8 *data_in,
622 struct page *dest_page, unsigned long dest_pgoff, size_t srclen,
625 struct workspace *workspace = list_entry(ws, struct workspace, list);
626 struct btrfs_fs_info *fs_info = btrfs_sb(dest_page->mapping->host->i_sb);
627 const u32 sectorsize = fs_info->sectorsize;
628 zstd_dstream *stream;
630 unsigned long to_copy = 0;
632 stream = zstd_init_dstream(
633 ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
635 pr_warn("BTRFS: zstd_init_dstream failed\n");
639 workspace->in_buf.src = data_in;
640 workspace->in_buf.pos = 0;
641 workspace->in_buf.size = srclen;
643 workspace->out_buf.dst = workspace->buf;
644 workspace->out_buf.pos = 0;
645 workspace->out_buf.size = sectorsize;
648 * Since both input and output buffers should not exceed one sector,
649 * one call should end the decompression.
651 ret = zstd_decompress_stream(stream, &workspace->out_buf, &workspace->in_buf);
652 if (zstd_is_error(ret)) {
653 pr_warn_ratelimited("BTRFS: zstd_decompress_stream return %d\n",
654 zstd_get_error_code(ret));
657 to_copy = workspace->out_buf.pos;
658 memcpy_to_page(dest_page, dest_pgoff, workspace->out_buf.dst, to_copy);
660 /* Error or early end. */
661 if (unlikely(to_copy < destlen)) {
663 memzero_page(dest_page, dest_pgoff + to_copy, destlen - to_copy);
668 const struct btrfs_compress_op btrfs_zstd_compress = {
669 /* ZSTD uses own workspace manager */
670 .workspace_manager = NULL,
671 .max_level = ZSTD_BTRFS_MAX_LEVEL,
672 .default_level = ZSTD_BTRFS_DEFAULT_LEVEL,