Skip to content

Commit 431e982

Browse files
committed
btrfs: move some zstd work data from stack to workspace
* ZSTD_inBuffer in_buf * ZSTD_outBuffer out_buf are used in all functions to pass the compression parameters and the local variables consume some space. We can move them to the workspace and reduce the stack consumption: zstd.c:zstd_decompress -24 (136 -> 112) zstd.c:zstd_decompress_bio -24 (144 -> 120) zstd.c:zstd_compress_pages -24 (264 -> 240) Signed-off-by: David Sterba <dsterba@suse.com> Reviewed-by: Nick Terrell <terrelln@fb.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 5302e08 commit 431e982

1 file changed

Lines changed: 67 additions & 65 deletions

File tree

‎fs/btrfs/zstd.c‎

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ struct workspace {
4343
size_t size;
4444
char *buf;
4545
struct list_head list;
46+
ZSTD_inBuffer in_buf;
47+
ZSTD_outBuffer out_buf;
4648
};
4749

4850
static void zstd_free_workspace(struct list_head *ws)
@@ -94,8 +96,6 @@ static int zstd_compress_pages(struct list_head *ws,
9496
int nr_pages = 0;
9597
struct page *in_page = NULL; /* The current page to read */
9698
struct page *out_page = NULL; /* The current page to write to */
97-
ZSTD_inBuffer in_buf = { NULL, 0, 0 };
98-
ZSTD_outBuffer out_buf = { NULL, 0, 0 };
9999
unsigned long tot_in = 0;
100100
unsigned long tot_out = 0;
101101
unsigned long len = *total_out;
@@ -118,9 +118,9 @@ static int zstd_compress_pages(struct list_head *ws,
118118

119119
/* map in the first page of input data */
120120
in_page = find_get_page(mapping, start >> PAGE_SHIFT);
121-
in_buf.src = kmap(in_page);
122-
in_buf.pos = 0;
123-
in_buf.size = min_t(size_t, len, PAGE_SIZE);
121+
workspace->in_buf.src = kmap(in_page);
122+
workspace->in_buf.pos = 0;
123+
workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
124124

125125

126126
/* Allocate and map in the output buffer */
@@ -130,14 +130,15 @@ static int zstd_compress_pages(struct list_head *ws,
130130
goto out;
131131
}
132132
pages[nr_pages++] = out_page;
133-
out_buf.dst = kmap(out_page);
134-
out_buf.pos = 0;
135-
out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
133+
workspace->out_buf.dst = kmap(out_page);
134+
workspace->out_buf.pos = 0;
135+
workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
136136

137137
while (1) {
138138
size_t ret2;
139139

140-
ret2 = ZSTD_compressStream(stream, &out_buf, &in_buf);
140+
ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
141+
&workspace->in_buf);
141142
if (ZSTD_isError(ret2)) {
142143
pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
143144
ZSTD_getErrorCode(ret2));
@@ -146,22 +147,22 @@ static int zstd_compress_pages(struct list_head *ws,
146147
}
147148

148149
/* Check to see if we are making it bigger */
149-
if (tot_in + in_buf.pos > 8192 &&
150-
tot_in + in_buf.pos <
151-
tot_out + out_buf.pos) {
150+
if (tot_in + workspace->in_buf.pos > 8192 &&
151+
tot_in + workspace->in_buf.pos <
152+
tot_out + workspace->out_buf.pos) {
152153
ret = -E2BIG;
153154
goto out;
154155
}
155156

156157
/* We've reached the end of our output range */
157-
if (out_buf.pos >= max_out) {
158-
tot_out += out_buf.pos;
158+
if (workspace->out_buf.pos >= max_out) {
159+
tot_out += workspace->out_buf.pos;
159160
ret = -E2BIG;
160161
goto out;
161162
}
162163

163164
/* Check if we need more output space */
164-
if (out_buf.pos == out_buf.size) {
165+
if (workspace->out_buf.pos == workspace->out_buf.size) {
165166
tot_out += PAGE_SIZE;
166167
max_out -= PAGE_SIZE;
167168
kunmap(out_page);
@@ -176,47 +177,48 @@ static int zstd_compress_pages(struct list_head *ws,
176177
goto out;
177178
}
178179
pages[nr_pages++] = out_page;
179-
out_buf.dst = kmap(out_page);
180-
out_buf.pos = 0;
181-
out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
180+
workspace->out_buf.dst = kmap(out_page);
181+
workspace->out_buf.pos = 0;
182+
workspace->out_buf.size = min_t(size_t, max_out,
183+
PAGE_SIZE);
182184
}
183185

184186
/* We've reached the end of the input */
185-
if (in_buf.pos >= len) {
186-
tot_in += in_buf.pos;
187+
if (workspace->in_buf.pos >= len) {
188+
tot_in += workspace->in_buf.pos;
187189
break;
188190
}
189191

190192
/* Check if we need more input */
191-
if (in_buf.pos == in_buf.size) {
193+
if (workspace->in_buf.pos == workspace->in_buf.size) {
192194
tot_in += PAGE_SIZE;
193195
kunmap(in_page);
194196
put_page(in_page);
195197

196198
start += PAGE_SIZE;
197199
len -= PAGE_SIZE;
198200
in_page = find_get_page(mapping, start >> PAGE_SHIFT);
199-
in_buf.src = kmap(in_page);
200-
in_buf.pos = 0;
201-
in_buf.size = min_t(size_t, len, PAGE_SIZE);
201+
workspace->in_buf.src = kmap(in_page);
202+
workspace->in_buf.pos = 0;
203+
workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
202204
}
203205
}
204206
while (1) {
205207
size_t ret2;
206208

207-
ret2 = ZSTD_endStream(stream, &out_buf);
209+
ret2 = ZSTD_endStream(stream, &workspace->out_buf);
208210
if (ZSTD_isError(ret2)) {
209211
pr_debug("BTRFS: ZSTD_endStream returned %d\n",
210212
ZSTD_getErrorCode(ret2));
211213
ret = -EIO;
212214
goto out;
213215
}
214216
if (ret2 == 0) {
215-
tot_out += out_buf.pos;
217+
tot_out += workspace->out_buf.pos;
216218
break;
217219
}
218-
if (out_buf.pos >= max_out) {
219-
tot_out += out_buf.pos;
220+
if (workspace->out_buf.pos >= max_out) {
221+
tot_out += workspace->out_buf.pos;
220222
ret = -E2BIG;
221223
goto out;
222224
}
@@ -235,9 +237,9 @@ static int zstd_compress_pages(struct list_head *ws,
235237
goto out;
236238
}
237239
pages[nr_pages++] = out_page;
238-
out_buf.dst = kmap(out_page);
239-
out_buf.pos = 0;
240-
out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
240+
workspace->out_buf.dst = kmap(out_page);
241+
workspace->out_buf.pos = 0;
242+
workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
241243
}
242244

243245
if (tot_out >= tot_in) {
@@ -273,8 +275,6 @@ static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
273275
unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
274276
unsigned long buf_start;
275277
unsigned long total_out = 0;
276-
ZSTD_inBuffer in_buf = { NULL, 0, 0 };
277-
ZSTD_outBuffer out_buf = { NULL, 0, 0 };
278278

279279
stream = ZSTD_initDStream(
280280
ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
@@ -284,57 +284,58 @@ static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
284284
goto done;
285285
}
286286

287-
in_buf.src = kmap(pages_in[page_in_index]);
288-
in_buf.pos = 0;
289-
in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
287+
workspace->in_buf.src = kmap(pages_in[page_in_index]);
288+
workspace->in_buf.pos = 0;
289+
workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
290290

291-
out_buf.dst = workspace->buf;
292-
out_buf.pos = 0;
293-
out_buf.size = PAGE_SIZE;
291+
workspace->out_buf.dst = workspace->buf;
292+
workspace->out_buf.pos = 0;
293+
workspace->out_buf.size = PAGE_SIZE;
294294

295295
while (1) {
296296
size_t ret2;
297297

298-
ret2 = ZSTD_decompressStream(stream, &out_buf, &in_buf);
298+
ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
299+
&workspace->in_buf);
299300
if (ZSTD_isError(ret2)) {
300301
pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
301302
ZSTD_getErrorCode(ret2));
302303
ret = -EIO;
303304
goto done;
304305
}
305306
buf_start = total_out;
306-
total_out += out_buf.pos;
307-
out_buf.pos = 0;
307+
total_out += workspace->out_buf.pos;
308+
workspace->out_buf.pos = 0;
308309

309-
ret = btrfs_decompress_buf2page(out_buf.dst, buf_start,
310-
total_out, disk_start, orig_bio);
310+
ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
311+
buf_start, total_out, disk_start, orig_bio);
311312
if (ret == 0)
312313
break;
313314

314-
if (in_buf.pos >= srclen)
315+
if (workspace->in_buf.pos >= srclen)
315316
break;
316317

317318
/* Check if we've hit the end of a frame */
318319
if (ret2 == 0)
319320
break;
320321

321-
if (in_buf.pos == in_buf.size) {
322+
if (workspace->in_buf.pos == workspace->in_buf.size) {
322323
kunmap(pages_in[page_in_index++]);
323324
if (page_in_index >= total_pages_in) {
324-
in_buf.src = NULL;
325+
workspace->in_buf.src = NULL;
325326
ret = -EIO;
326327
goto done;
327328
}
328329
srclen -= PAGE_SIZE;
329-
in_buf.src = kmap(pages_in[page_in_index]);
330-
in_buf.pos = 0;
331-
in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
330+
workspace->in_buf.src = kmap(pages_in[page_in_index]);
331+
workspace->in_buf.pos = 0;
332+
workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
332333
}
333334
}
334335
ret = 0;
335336
zero_fill_bio(orig_bio);
336337
done:
337-
if (in_buf.src)
338+
if (workspace->in_buf.src)
338339
kunmap(pages_in[page_in_index]);
339340
return ret;
340341
}
@@ -348,8 +349,6 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
348349
ZSTD_DStream *stream;
349350
int ret = 0;
350351
size_t ret2;
351-
ZSTD_inBuffer in_buf = { NULL, 0, 0 };
352-
ZSTD_outBuffer out_buf = { NULL, 0, 0 };
353352
unsigned long total_out = 0;
354353
unsigned long pg_offset = 0;
355354
char *kaddr;
@@ -364,16 +363,17 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
364363

365364
destlen = min_t(size_t, destlen, PAGE_SIZE);
366365

367-
in_buf.src = data_in;
368-
in_buf.pos = 0;
369-
in_buf.size = srclen;
366+
workspace->in_buf.src = data_in;
367+
workspace->in_buf.pos = 0;
368+
workspace->in_buf.size = srclen;
370369

371-
out_buf.dst = workspace->buf;
372-
out_buf.pos = 0;
373-
out_buf.size = PAGE_SIZE;
370+
workspace->out_buf.dst = workspace->buf;
371+
workspace->out_buf.pos = 0;
372+
workspace->out_buf.size = PAGE_SIZE;
374373

375374
ret2 = 1;
376-
while (pg_offset < destlen && in_buf.pos < in_buf.size) {
375+
while (pg_offset < destlen
376+
&& workspace->in_buf.pos < workspace->in_buf.size) {
377377
unsigned long buf_start;
378378
unsigned long buf_offset;
379379
unsigned long bytes;
@@ -384,7 +384,8 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
384384
ret = -EIO;
385385
goto finish;
386386
}
387-
ret2 = ZSTD_decompressStream(stream, &out_buf, &in_buf);
387+
ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
388+
&workspace->in_buf);
388389
if (ZSTD_isError(ret2)) {
389390
pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
390391
ZSTD_getErrorCode(ret2));
@@ -393,8 +394,8 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
393394
}
394395

395396
buf_start = total_out;
396-
total_out += out_buf.pos;
397-
out_buf.pos = 0;
397+
total_out += workspace->out_buf.pos;
398+
workspace->out_buf.pos = 0;
398399

399400
if (total_out <= start_byte)
400401
continue;
@@ -405,10 +406,11 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
405406
buf_offset = 0;
406407

407408
bytes = min_t(unsigned long, destlen - pg_offset,
408-
out_buf.size - buf_offset);
409+
workspace->out_buf.size - buf_offset);
409410

410411
kaddr = kmap_atomic(dest_page);
411-
memcpy(kaddr + pg_offset, out_buf.dst + buf_offset, bytes);
412+
memcpy(kaddr + pg_offset, workspace->out_buf.dst + buf_offset,
413+
bytes);
412414
kunmap_atomic(kaddr);
413415

414416
pg_offset += bytes;

0 commit comments

Comments
 (0)