Skip to content

Commit 3f93aef

Browse files
dennisszhoukdave
authored andcommitted
btrfs: add zstd compression level support
Zstd compression requires different amounts of memory for each level of compression. The prior patches implemented indirection to allow for each compression type to manage their workspaces independently. This patch uses this indirection to implement compression level support for zstd. To manage the additional memory require, each compression level has its own queue of workspaces. A global LRU is used to help with reclaim. Reclaim is done via a timer which provides a mechanism to decrease memory utilization by keeping only workspaces around that are sized appropriately. Forward progress is guaranteed by a preallocated max workspace hidden from the LRU. When getting a workspace, it uses a bitmap to identify the levels that are populated and scans up. If it finds a workspace that is greater than it, it uses it, but does not update the last_used time and the corresponding place in the LRU. If we hit memory pressure, we sleep on the max level workspace. We continue to rescan in case we can use a smaller workspace, but eventually should be able to obtain the max level workspace or allocate one again should memory pressure subside. The memory requirement for decompression is the same as level 1, and therefore can use any of available workspace. The number of workspaces is bound by an upper limit of the workqueue's limit which currently is 2 (percpu limit). The reclaim timer is used to free inactive/improperly sized workspaces and is set to 307s to avoid colliding with transaction commit (every 30s). Repeating the experiment from v2 [1], the Silesia corpus was copied to a btrfs filesystem 10 times and then read back after dropping the caches. The btrfs filesystem was on an SSD. Level Ratio Compression (MB/s) Decompression (MB/s) Memory (KB) 1 2.658 438.47 910.51 780 2 2.744 364.86 886.55 1004 3 2.801 336.33 828.41 1260 4 2.858 286.71 886.55 1260 5 2.916 212.77 556.84 1388 6 2.363 119.82 990.85 1516 7 3.000 154.06 849.30 1516 8 3.011 159.54 875.03 1772 9 3.025 100.51 940.15 1772 10 3.033 118.97 616.26 1772 11 3.036 94.19 802.11 1772 12 3.037 73.45 931.49 1772 13 3.041 55.17 835.26 2284 14 3.087 44.70 716.78 2547 15 3.126 37.30 878.84 2547 [1] https://lore.kernel.org/linux-btrfs/20181031181108.289340-1-terrelln@fb.com/ Cc: Nick Terrell <terrelln@fb.com> Cc: Omar Sandoval <osandov@osandov.com> Signed-off-by: Dennis Zhou <dennis@kernel.org> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent d3c6ab7 commit 3f93aef

2 files changed

Lines changed: 245 additions & 9 deletions

File tree

‎fs/btrfs/super.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,13 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
544544
btrfs_clear_opt(info->mount_opt, NODATASUM);
545545
btrfs_set_fs_incompat(info, COMPRESS_LZO);
546546
no_compress = 0;
547-
} else if (strcmp(args[0].from, "zstd") == 0) {
547+
} else if (strncmp(args[0].from, "zstd", 4) == 0) {
548548
compress_type = "zstd";
549549
info->compress_type = BTRFS_COMPRESS_ZSTD;
550+
info->compress_level =
551+
btrfs_compress_str2level(
552+
BTRFS_COMPRESS_ZSTD,
553+
args[0].from + 4);
550554
btrfs_set_opt(info->mount_opt, COMPRESS);
551555
btrfs_clear_opt(info->mount_opt, NODATACOW);
552556
btrfs_clear_opt(info->mount_opt, NODATASUM);

‎fs/btrfs/zstd.c‎

Lines changed: 240 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@
66
*/
77

88
#include <linux/bio.h>
9+
#include <linux/bitmap.h>
910
#include <linux/err.h>
1011
#include <linux/init.h>
1112
#include <linux/kernel.h>
1213
#include <linux/mm.h>
14+
#include <linux/sched/mm.h>
1315
#include <linux/pagemap.h>
1416
#include <linux/refcount.h>
1517
#include <linux/sched.h>
1618
#include <linux/slab.h>
1719
#include <linux/zstd.h>
1820
#include "compression.h"
21+
#include "ctree.h"
1922

2023
#define ZSTD_BTRFS_MAX_WINDOWLOG 17
2124
#define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
2225
#define ZSTD_BTRFS_DEFAULT_LEVEL 3
2326
#define ZSTD_BTRFS_MAX_LEVEL 15
27+
/* 307s to avoid pathologically clashing with transaction commit */
28+
#define ZSTD_BTRFS_RECLAIM_JIFFIES (307 * HZ)
2429

2530
static ZSTD_parameters zstd_get_btrfs_parameters(unsigned int level,
2631
size_t src_len)
@@ -37,16 +42,101 @@ struct workspace {
3742
void *mem;
3843
size_t size;
3944
char *buf;
45+
unsigned int level;
4046
unsigned int req_level;
47+
unsigned long last_used; /* jiffies */
4148
struct list_head list;
49+
struct list_head lru_list;
4250
ZSTD_inBuffer in_buf;
4351
ZSTD_outBuffer out_buf;
4452
};
4553

46-
static struct workspace_manager wsm;
54+
/*
55+
* Zstd Workspace Management
56+
*
57+
* Zstd workspaces have different memory requirements depending on the level.
58+
* The zstd workspaces are managed by having individual lists for each level
59+
* and a global lru. Forward progress is maintained by protecting a max level
60+
* workspace.
61+
*
62+
* Getting a workspace is done by using the bitmap to identify the levels that
63+
* have available workspaces and scans up. This lets us recycle higher level
64+
* workspaces because of the monotonic memory guarantee. A workspace's
65+
* last_used is only updated if it is being used by the corresponding memory
66+
* level. Putting a workspace involves adding it back to the appropriate places
67+
* and adding it back to the lru if necessary.
68+
*
69+
* A timer is used to reclaim workspaces if they have not been used for
70+
* ZSTD_BTRFS_RECLAIM_JIFFIES. This helps keep only active workspaces around.
71+
* The upper bound is provided by the workqueue limit which is 2 (percpu limit).
72+
*/
73+
74+
struct zstd_workspace_manager {
75+
const struct btrfs_compress_op *ops;
76+
spinlock_t lock;
77+
struct list_head lru_list;
78+
struct list_head idle_ws[ZSTD_BTRFS_MAX_LEVEL];
79+
unsigned long active_map;
80+
wait_queue_head_t wait;
81+
struct timer_list timer;
82+
};
83+
84+
static struct zstd_workspace_manager wsm;
4785

4886
static size_t zstd_ws_mem_sizes[ZSTD_BTRFS_MAX_LEVEL];
4987

88+
static inline struct workspace *list_to_workspace(struct list_head *list)
89+
{
90+
return container_of(list, struct workspace, list);
91+
}
92+
93+
/*
94+
* zstd_reclaim_timer_fn - reclaim timer
95+
* @t: timer
96+
*
97+
* This scans the lru_list and attempts to reclaim any workspace that hasn't
98+
* been used for ZSTD_BTRFS_RECLAIM_JIFFIES.
99+
*/
100+
static void zstd_reclaim_timer_fn(struct timer_list *timer)
101+
{
102+
unsigned long reclaim_threshold = jiffies - ZSTD_BTRFS_RECLAIM_JIFFIES;
103+
struct list_head *pos, *next;
104+
105+
spin_lock(&wsm.lock);
106+
107+
if (list_empty(&wsm.lru_list)) {
108+
spin_unlock(&wsm.lock);
109+
return;
110+
}
111+
112+
list_for_each_prev_safe(pos, next, &wsm.lru_list) {
113+
struct workspace *victim = container_of(pos, struct workspace,
114+
lru_list);
115+
unsigned int level;
116+
117+
if (time_after(victim->last_used, reclaim_threshold))
118+
break;
119+
120+
/* workspace is in use */
121+
if (victim->req_level)
122+
continue;
123+
124+
level = victim->level;
125+
list_del(&victim->lru_list);
126+
list_del(&victim->list);
127+
wsm.ops->free_workspace(&victim->list);
128+
129+
if (list_empty(&wsm.idle_ws[level - 1]))
130+
clear_bit(level - 1, &wsm.active_map);
131+
132+
}
133+
134+
if (!list_empty(&wsm.lru_list))
135+
mod_timer(&wsm.timer, jiffies + ZSTD_BTRFS_RECLAIM_JIFFIES);
136+
137+
spin_unlock(&wsm.lock);
138+
}
139+
50140
/*
51141
* zstd_calc_ws_mem_sizes - calculate monotonic memory bounds
52142
*
@@ -76,29 +166,164 @@ static void zstd_calc_ws_mem_sizes(void)
76166

77167
static void zstd_init_workspace_manager(void)
78168
{
169+
struct list_head *ws;
170+
int i;
171+
79172
zstd_calc_ws_mem_sizes();
80173

81-
btrfs_init_workspace_manager(&wsm, &btrfs_zstd_compress);
174+
wsm.ops = &btrfs_zstd_compress;
175+
spin_lock_init(&wsm.lock);
176+
init_waitqueue_head(&wsm.wait);
177+
timer_setup(&wsm.timer, zstd_reclaim_timer_fn, 0);
178+
179+
INIT_LIST_HEAD(&wsm.lru_list);
180+
for (i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++)
181+
INIT_LIST_HEAD(&wsm.idle_ws[i]);
182+
183+
ws = wsm.ops->alloc_workspace(ZSTD_BTRFS_MAX_LEVEL);
184+
if (IS_ERR(ws)) {
185+
pr_warn(
186+
"BTRFS: cannot preallocate zstd compression workspace\n");
187+
} else {
188+
set_bit(ZSTD_BTRFS_MAX_LEVEL - 1, &wsm.active_map);
189+
list_add(ws, &wsm.idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1]);
190+
}
82191
}
83192

84193
static void zstd_cleanup_workspace_manager(void)
85194
{
86-
btrfs_cleanup_workspace_manager(&wsm);
195+
struct workspace *workspace;
196+
int i;
197+
198+
del_timer(&wsm.timer);
199+
200+
for (i = 0; i < ZSTD_BTRFS_MAX_LEVEL; i++) {
201+
while (!list_empty(&wsm.idle_ws[i])) {
202+
workspace = container_of(wsm.idle_ws[i].next,
203+
struct workspace, list);
204+
list_del(&workspace->list);
205+
list_del(&workspace->lru_list);
206+
wsm.ops->free_workspace(&workspace->list);
207+
}
208+
}
209+
}
210+
211+
/*
212+
* zstd_find_workspace - find workspace
213+
* @level: compression level
214+
*
215+
* This iterates over the set bits in the active_map beginning at the requested
216+
* compression level. This lets us utilize already allocated workspaces before
217+
* allocating a new one. If the workspace is of a larger size, it is used, but
218+
* the place in the lru_list and last_used times are not updated. This is to
219+
* offer the opportunity to reclaim the workspace in favor of allocating an
220+
* appropriately sized one in the future.
221+
*/
222+
static struct list_head *zstd_find_workspace(unsigned int level)
223+
{
224+
struct list_head *ws;
225+
struct workspace *workspace;
226+
int i = level - 1;
227+
228+
spin_lock(&wsm.lock);
229+
for_each_set_bit_from(i, &wsm.active_map, ZSTD_BTRFS_MAX_LEVEL) {
230+
if (!list_empty(&wsm.idle_ws[i])) {
231+
ws = wsm.idle_ws[i].next;
232+
workspace = list_to_workspace(ws);
233+
list_del_init(ws);
234+
/* keep its place if it's a lower level using this */
235+
workspace->req_level = level;
236+
if (level == workspace->level)
237+
list_del(&workspace->lru_list);
238+
if (list_empty(&wsm.idle_ws[i]))
239+
clear_bit(i, &wsm.active_map);
240+
spin_unlock(&wsm.lock);
241+
return ws;
242+
}
243+
}
244+
spin_unlock(&wsm.lock);
245+
246+
return NULL;
87247
}
88248

249+
/*
250+
* zstd_get_workspace - zstd's get_workspace
251+
* @level: compression level
252+
*
253+
* If @level is 0, then any compression level can be used. Therefore, we begin
254+
* scanning from 1. We first scan through possible workspaces and then after
255+
* attempt to allocate a new workspace. If we fail to allocate one due to
256+
* memory pressure, go to sleep waiting for the max level workspace to free up.
257+
*/
89258
static struct list_head *zstd_get_workspace(unsigned int level)
90259
{
91-
struct list_head *ws = btrfs_get_workspace(&wsm, level);
92-
struct workspace *workspace = list_entry(ws, struct workspace, list);
260+
struct list_head *ws;
261+
unsigned int nofs_flag;
93262

94-
workspace->req_level = level;
263+
/* level == 0 means we can use any workspace */
264+
if (!level)
265+
level = 1;
266+
267+
again:
268+
ws = zstd_find_workspace(level);
269+
if (ws)
270+
return ws;
271+
272+
nofs_flag = memalloc_nofs_save();
273+
ws = wsm.ops->alloc_workspace(level);
274+
memalloc_nofs_restore(nofs_flag);
275+
276+
if (IS_ERR(ws)) {
277+
DEFINE_WAIT(wait);
278+
279+
prepare_to_wait(&wsm.wait, &wait, TASK_UNINTERRUPTIBLE);
280+
schedule();
281+
finish_wait(&wsm.wait, &wait);
282+
283+
goto again;
284+
}
95285

96286
return ws;
97287
}
98288

289+
/*
290+
* zstd_put_workspace - zstd put_workspace
291+
* @ws: list_head for the workspace
292+
*
293+
* When putting back a workspace, we only need to update the LRU if we are of
294+
* the requested compression level. Here is where we continue to protect the
295+
* max level workspace or update last_used accordingly. If the reclaim timer
296+
* isn't set, it is also set here. Only the max level workspace tries and wakes
297+
* up waiting workspaces.
298+
*/
99299
static void zstd_put_workspace(struct list_head *ws)
100300
{
101-
btrfs_put_workspace(&wsm, ws);
301+
struct workspace *workspace = list_to_workspace(ws);
302+
303+
spin_lock(&wsm.lock);
304+
305+
/* A node is only taken off the lru if we are the corresponding level */
306+
if (workspace->req_level == workspace->level) {
307+
/* Hide a max level workspace from reclaim */
308+
if (list_empty(&wsm.idle_ws[ZSTD_BTRFS_MAX_LEVEL - 1])) {
309+
INIT_LIST_HEAD(&workspace->lru_list);
310+
} else {
311+
workspace->last_used = jiffies;
312+
list_add(&workspace->lru_list, &wsm.lru_list);
313+
if (!timer_pending(&wsm.timer))
314+
mod_timer(&wsm.timer,
315+
jiffies + ZSTD_BTRFS_RECLAIM_JIFFIES);
316+
}
317+
}
318+
319+
set_bit(workspace->level - 1, &wsm.active_map);
320+
list_add(&workspace->list, &wsm.idle_ws[workspace->level - 1]);
321+
workspace->req_level = 0;
322+
323+
spin_unlock(&wsm.lock);
324+
325+
if (workspace->level == ZSTD_BTRFS_MAX_LEVEL)
326+
cond_wake_up(&wsm.wait);
102327
}
103328

104329
static void zstd_free_workspace(struct list_head *ws)
@@ -119,12 +344,16 @@ static struct list_head *zstd_alloc_workspace(unsigned int level)
119344
return ERR_PTR(-ENOMEM);
120345

121346
workspace->size = zstd_ws_mem_sizes[level - 1];
347+
workspace->level = level;
348+
workspace->req_level = level;
349+
workspace->last_used = jiffies;
122350
workspace->mem = kvmalloc(workspace->size, GFP_KERNEL);
123351
workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
124352
if (!workspace->mem || !workspace->buf)
125353
goto fail;
126354

127355
INIT_LIST_HEAD(&workspace->list);
356+
INIT_LIST_HEAD(&workspace->lru_list);
128357

129358
return &workspace->list;
130359
fail:
@@ -478,7 +707,10 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
478707

479708
static unsigned int zstd_set_level(unsigned int level)
480709
{
481-
return ZSTD_BTRFS_DEFAULT_LEVEL;
710+
if (!level)
711+
return ZSTD_BTRFS_DEFAULT_LEVEL;
712+
713+
return min_t(unsigned int, level, ZSTD_BTRFS_MAX_LEVEL);
482714
}
483715

484716
const struct btrfs_compress_op btrfs_zstd_compress = {

0 commit comments

Comments
 (0)