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>
This commit is contained in:
David Sterba 2017-11-15 18:27:39 +01:00
parent 5302e08964
commit 431e98226c

View File

@ -43,6 +43,8 @@ struct workspace {
size_t size; size_t size;
char *buf; char *buf;
struct list_head list; struct list_head list;
ZSTD_inBuffer in_buf;
ZSTD_outBuffer out_buf;
}; };
static void zstd_free_workspace(struct list_head *ws) static void zstd_free_workspace(struct list_head *ws)
@ -94,8 +96,6 @@ static int zstd_compress_pages(struct list_head *ws,
int nr_pages = 0; int nr_pages = 0;
struct page *in_page = NULL; /* The current page to read */ struct page *in_page = NULL; /* The current page to read */
struct page *out_page = NULL; /* The current page to write to */ struct page *out_page = NULL; /* The current page to write to */
ZSTD_inBuffer in_buf = { NULL, 0, 0 };
ZSTD_outBuffer out_buf = { NULL, 0, 0 };
unsigned long tot_in = 0; unsigned long tot_in = 0;
unsigned long tot_out = 0; unsigned long tot_out = 0;
unsigned long len = *total_out; unsigned long len = *total_out;
@ -118,9 +118,9 @@ static int zstd_compress_pages(struct list_head *ws,
/* map in the first page of input data */ /* map in the first page of input data */
in_page = find_get_page(mapping, start >> PAGE_SHIFT); in_page = find_get_page(mapping, start >> PAGE_SHIFT);
in_buf.src = kmap(in_page); workspace->in_buf.src = kmap(in_page);
in_buf.pos = 0; workspace->in_buf.pos = 0;
in_buf.size = min_t(size_t, len, PAGE_SIZE); workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
/* Allocate and map in the output buffer */ /* Allocate and map in the output buffer */
@ -130,14 +130,15 @@ static int zstd_compress_pages(struct list_head *ws,
goto out; goto out;
} }
pages[nr_pages++] = out_page; pages[nr_pages++] = out_page;
out_buf.dst = kmap(out_page); workspace->out_buf.dst = kmap(out_page);
out_buf.pos = 0; workspace->out_buf.pos = 0;
out_buf.size = min_t(size_t, max_out, PAGE_SIZE); workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
while (1) { while (1) {
size_t ret2; size_t ret2;
ret2 = ZSTD_compressStream(stream, &out_buf, &in_buf); ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
&workspace->in_buf);
if (ZSTD_isError(ret2)) { if (ZSTD_isError(ret2)) {
pr_debug("BTRFS: ZSTD_compressStream returned %d\n", pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
ZSTD_getErrorCode(ret2)); ZSTD_getErrorCode(ret2));
@ -146,22 +147,22 @@ static int zstd_compress_pages(struct list_head *ws,
} }
/* Check to see if we are making it bigger */ /* Check to see if we are making it bigger */
if (tot_in + in_buf.pos > 8192 && if (tot_in + workspace->in_buf.pos > 8192 &&
tot_in + in_buf.pos < tot_in + workspace->in_buf.pos <
tot_out + out_buf.pos) { tot_out + workspace->out_buf.pos) {
ret = -E2BIG; ret = -E2BIG;
goto out; goto out;
} }
/* We've reached the end of our output range */ /* We've reached the end of our output range */
if (out_buf.pos >= max_out) { if (workspace->out_buf.pos >= max_out) {
tot_out += out_buf.pos; tot_out += workspace->out_buf.pos;
ret = -E2BIG; ret = -E2BIG;
goto out; goto out;
} }
/* Check if we need more output space */ /* Check if we need more output space */
if (out_buf.pos == out_buf.size) { if (workspace->out_buf.pos == workspace->out_buf.size) {
tot_out += PAGE_SIZE; tot_out += PAGE_SIZE;
max_out -= PAGE_SIZE; max_out -= PAGE_SIZE;
kunmap(out_page); kunmap(out_page);
@ -176,19 +177,20 @@ static int zstd_compress_pages(struct list_head *ws,
goto out; goto out;
} }
pages[nr_pages++] = out_page; pages[nr_pages++] = out_page;
out_buf.dst = kmap(out_page); workspace->out_buf.dst = kmap(out_page);
out_buf.pos = 0; workspace->out_buf.pos = 0;
out_buf.size = min_t(size_t, max_out, PAGE_SIZE); workspace->out_buf.size = min_t(size_t, max_out,
PAGE_SIZE);
} }
/* We've reached the end of the input */ /* We've reached the end of the input */
if (in_buf.pos >= len) { if (workspace->in_buf.pos >= len) {
tot_in += in_buf.pos; tot_in += workspace->in_buf.pos;
break; break;
} }
/* Check if we need more input */ /* Check if we need more input */
if (in_buf.pos == in_buf.size) { if (workspace->in_buf.pos == workspace->in_buf.size) {
tot_in += PAGE_SIZE; tot_in += PAGE_SIZE;
kunmap(in_page); kunmap(in_page);
put_page(in_page); put_page(in_page);
@ -196,15 +198,15 @@ static int zstd_compress_pages(struct list_head *ws,
start += PAGE_SIZE; start += PAGE_SIZE;
len -= PAGE_SIZE; len -= PAGE_SIZE;
in_page = find_get_page(mapping, start >> PAGE_SHIFT); in_page = find_get_page(mapping, start >> PAGE_SHIFT);
in_buf.src = kmap(in_page); workspace->in_buf.src = kmap(in_page);
in_buf.pos = 0; workspace->in_buf.pos = 0;
in_buf.size = min_t(size_t, len, PAGE_SIZE); workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
} }
} }
while (1) { while (1) {
size_t ret2; size_t ret2;
ret2 = ZSTD_endStream(stream, &out_buf); ret2 = ZSTD_endStream(stream, &workspace->out_buf);
if (ZSTD_isError(ret2)) { if (ZSTD_isError(ret2)) {
pr_debug("BTRFS: ZSTD_endStream returned %d\n", pr_debug("BTRFS: ZSTD_endStream returned %d\n",
ZSTD_getErrorCode(ret2)); ZSTD_getErrorCode(ret2));
@ -212,11 +214,11 @@ static int zstd_compress_pages(struct list_head *ws,
goto out; goto out;
} }
if (ret2 == 0) { if (ret2 == 0) {
tot_out += out_buf.pos; tot_out += workspace->out_buf.pos;
break; break;
} }
if (out_buf.pos >= max_out) { if (workspace->out_buf.pos >= max_out) {
tot_out += out_buf.pos; tot_out += workspace->out_buf.pos;
ret = -E2BIG; ret = -E2BIG;
goto out; goto out;
} }
@ -235,9 +237,9 @@ static int zstd_compress_pages(struct list_head *ws,
goto out; goto out;
} }
pages[nr_pages++] = out_page; pages[nr_pages++] = out_page;
out_buf.dst = kmap(out_page); workspace->out_buf.dst = kmap(out_page);
out_buf.pos = 0; workspace->out_buf.pos = 0;
out_buf.size = min_t(size_t, max_out, PAGE_SIZE); workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
} }
if (tot_out >= tot_in) { if (tot_out >= tot_in) {
@ -273,8 +275,6 @@ static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE); unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
unsigned long buf_start; unsigned long buf_start;
unsigned long total_out = 0; unsigned long total_out = 0;
ZSTD_inBuffer in_buf = { NULL, 0, 0 };
ZSTD_outBuffer out_buf = { NULL, 0, 0 };
stream = ZSTD_initDStream( stream = ZSTD_initDStream(
ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size); ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
@ -284,18 +284,19 @@ static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
goto done; goto done;
} }
in_buf.src = kmap(pages_in[page_in_index]); workspace->in_buf.src = kmap(pages_in[page_in_index]);
in_buf.pos = 0; workspace->in_buf.pos = 0;
in_buf.size = min_t(size_t, srclen, PAGE_SIZE); workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
out_buf.dst = workspace->buf; workspace->out_buf.dst = workspace->buf;
out_buf.pos = 0; workspace->out_buf.pos = 0;
out_buf.size = PAGE_SIZE; workspace->out_buf.size = PAGE_SIZE;
while (1) { while (1) {
size_t ret2; size_t ret2;
ret2 = ZSTD_decompressStream(stream, &out_buf, &in_buf); ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
&workspace->in_buf);
if (ZSTD_isError(ret2)) { if (ZSTD_isError(ret2)) {
pr_debug("BTRFS: ZSTD_decompressStream returned %d\n", pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
ZSTD_getErrorCode(ret2)); ZSTD_getErrorCode(ret2));
@ -303,38 +304,38 @@ static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
goto done; goto done;
} }
buf_start = total_out; buf_start = total_out;
total_out += out_buf.pos; total_out += workspace->out_buf.pos;
out_buf.pos = 0; workspace->out_buf.pos = 0;
ret = btrfs_decompress_buf2page(out_buf.dst, buf_start, ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
total_out, disk_start, orig_bio); buf_start, total_out, disk_start, orig_bio);
if (ret == 0) if (ret == 0)
break; break;
if (in_buf.pos >= srclen) if (workspace->in_buf.pos >= srclen)
break; break;
/* Check if we've hit the end of a frame */ /* Check if we've hit the end of a frame */
if (ret2 == 0) if (ret2 == 0)
break; break;
if (in_buf.pos == in_buf.size) { if (workspace->in_buf.pos == workspace->in_buf.size) {
kunmap(pages_in[page_in_index++]); kunmap(pages_in[page_in_index++]);
if (page_in_index >= total_pages_in) { if (page_in_index >= total_pages_in) {
in_buf.src = NULL; workspace->in_buf.src = NULL;
ret = -EIO; ret = -EIO;
goto done; goto done;
} }
srclen -= PAGE_SIZE; srclen -= PAGE_SIZE;
in_buf.src = kmap(pages_in[page_in_index]); workspace->in_buf.src = kmap(pages_in[page_in_index]);
in_buf.pos = 0; workspace->in_buf.pos = 0;
in_buf.size = min_t(size_t, srclen, PAGE_SIZE); workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
} }
} }
ret = 0; ret = 0;
zero_fill_bio(orig_bio); zero_fill_bio(orig_bio);
done: done:
if (in_buf.src) if (workspace->in_buf.src)
kunmap(pages_in[page_in_index]); kunmap(pages_in[page_in_index]);
return ret; return ret;
} }
@ -348,8 +349,6 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
ZSTD_DStream *stream; ZSTD_DStream *stream;
int ret = 0; int ret = 0;
size_t ret2; size_t ret2;
ZSTD_inBuffer in_buf = { NULL, 0, 0 };
ZSTD_outBuffer out_buf = { NULL, 0, 0 };
unsigned long total_out = 0; unsigned long total_out = 0;
unsigned long pg_offset = 0; unsigned long pg_offset = 0;
char *kaddr; char *kaddr;
@ -364,16 +363,17 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
destlen = min_t(size_t, destlen, PAGE_SIZE); destlen = min_t(size_t, destlen, PAGE_SIZE);
in_buf.src = data_in; workspace->in_buf.src = data_in;
in_buf.pos = 0; workspace->in_buf.pos = 0;
in_buf.size = srclen; workspace->in_buf.size = srclen;
out_buf.dst = workspace->buf; workspace->out_buf.dst = workspace->buf;
out_buf.pos = 0; workspace->out_buf.pos = 0;
out_buf.size = PAGE_SIZE; workspace->out_buf.size = PAGE_SIZE;
ret2 = 1; ret2 = 1;
while (pg_offset < destlen && in_buf.pos < in_buf.size) { while (pg_offset < destlen
&& workspace->in_buf.pos < workspace->in_buf.size) {
unsigned long buf_start; unsigned long buf_start;
unsigned long buf_offset; unsigned long buf_offset;
unsigned long bytes; unsigned long bytes;
@ -384,7 +384,8 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
ret = -EIO; ret = -EIO;
goto finish; goto finish;
} }
ret2 = ZSTD_decompressStream(stream, &out_buf, &in_buf); ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
&workspace->in_buf);
if (ZSTD_isError(ret2)) { if (ZSTD_isError(ret2)) {
pr_debug("BTRFS: ZSTD_decompressStream returned %d\n", pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
ZSTD_getErrorCode(ret2)); ZSTD_getErrorCode(ret2));
@ -393,8 +394,8 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
} }
buf_start = total_out; buf_start = total_out;
total_out += out_buf.pos; total_out += workspace->out_buf.pos;
out_buf.pos = 0; workspace->out_buf.pos = 0;
if (total_out <= start_byte) if (total_out <= start_byte)
continue; continue;
@ -405,10 +406,11 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
buf_offset = 0; buf_offset = 0;
bytes = min_t(unsigned long, destlen - pg_offset, bytes = min_t(unsigned long, destlen - pg_offset,
out_buf.size - buf_offset); workspace->out_buf.size - buf_offset);
kaddr = kmap_atomic(dest_page); kaddr = kmap_atomic(dest_page);
memcpy(kaddr + pg_offset, out_buf.dst + buf_offset, bytes); memcpy(kaddr + pg_offset, workspace->out_buf.dst + buf_offset,
bytes);
kunmap_atomic(kaddr); kunmap_atomic(kaddr);
pg_offset += bytes; pg_offset += bytes;